From 6d11bdafea0cc119d70a358e6e15d1b99c84294f Mon Sep 17 00:00:00 2001 From: Daniel Opitz Date: Wed, 17 Jul 2024 19:32:45 +0200 Subject: [PATCH] Update usage with DI container (#323) --- README.md | 59 +++++++++++++++++++++++++++++++------------------------ 1 file changed, 33 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index dbb48a1..dee178f 100644 --- a/README.md +++ b/README.md @@ -20,45 +20,55 @@ Requires Slim Framework 4, Twig 3 and PHP 7.4 or newer. ## Usage +### With DI Container + ```php use DI\Container; use Slim\Factory\AppFactory; use Slim\Views\Twig; use Slim\Views\TwigMiddleware; -require __DIR__ . '/vendor/autoload.php'; +require __DIR__ . '/../vendor/autoload.php'; // Create Container $container = new Container(); -AppFactory::setContainer($container); // Set view in Container -$container->set('view', function() { - return Twig::create('path/to/templates', ['cache' => 'path/to/cache']); +$container->set(Twig::class, function() { + return Twig::create(__DIR__ . '/../templates', ['cache' => 'path/to/cache']); }); -// Create App -$app = AppFactory::create(); +// Create App from container +$app = AppFactory::createFromContainer($container); // Add Twig-View Middleware -$app->add(TwigMiddleware::createFromContainer($app)); +$app->add(TwigMiddleware::create($app, $container->get(Twig::class))); -// Define named route +// Add other middleware +$app->addRoutingMiddleware(); +$app->addErrorMiddleware(true, true, true); + +// Render from template file templates/profile.html.twig $app->get('/hello/{name}', function ($request, $response, $args) { - return $this->get('view')->render($response, 'profile.html', [ - 'name' => $args['name'] - ]); + $viewData = [ + 'name' => $args['name'], + ]; + + $twig = $this->get(Twig::class); + + return $twig->render($response, 'profile.html.twig', $viewData); })->setName('profile'); // Render from string $app->get('/hi/{name}', function ($request, $response, $args) { - $str = $this->get('view')->fetchFromString( - '

Hi, my name is {{ name }}.

', - [ - 'name' => $args['name'] - ] - ); + $viewData = [ + 'name' => $args['name'], + ]; + + $twig = $this->get(Twig::class); + $str = $twig->fetchFromString('

Hi, my name is {{ name }}.

', $viewData); $response->getBody()->write($str); + return $response; }); @@ -66,14 +76,14 @@ $app->get('/hi/{name}', function ($request, $response, $args) { $app->run(); ``` -### Without container +### Without DI container ```php use Slim\Factory\AppFactory; use Slim\Views\Twig; use Slim\Views\TwigMiddleware; -require __DIR__ . '/vendor/autoload.php'; +require __DIR__ . '/../vendor/autoload.php'; // Create App $app = AppFactory::create(); @@ -87,7 +97,7 @@ $app->add(TwigMiddleware::create($app, $twig)); // Define named route $app->get('/hello/{name}', function ($request, $response, $args) { $view = Twig::fromRequest($request); - return $view->render($response, 'profile.html', [ + return $view->render($response, 'profile.html.twig', [ 'name' => $args['name'] ]); })->setName('profile'); @@ -102,6 +112,7 @@ $app->get('/hi/{name}', function ($request, $response, $args) { ] ); $response->getBody()->write($str); + return $response; }); @@ -114,7 +125,7 @@ $app->run(); `TwigExtension` provides these functions to your Twig templates: * `url_for()` - returns the URL for a given route. e.g.: /hello/world -* `full_url_for()` - returns the URL for a given route. e.g.: http://www.example.com/hello/world +* `full_url_for()` - returns the URL for a given route. e.g.: https://www.example.com/hello/world * `is_current_url()` - returns true is the provided route name and parameters are valid for the current path. * `current_url()` - returns the current path, with or without the query string. * `get_uri()` - returns the `UriInterface` object from the incoming `ServerRequestInterface` object @@ -122,16 +133,12 @@ $app->run(); You can use `url_for` to generate complete URLs to any Slim application named route and use `is_current_url` to determine if you need to mark a link as active as shown in this example Twig template: -```php -{% extends "layout.html" %} - -{% block body %} +```html

User List

-{% endblock %} ``` ## Tests