Adding Twig to Slim4 Skeleton

I am new to Slim Framework. I installed the Home - Slim 4 Skeleton. I have been looking over the forum trying to figure out how to add twig. I ran the composer command to install twig.

In the bootstrap.php file I added this.

//Set view in Container
$container->set('view', function() {
   return Twig::create(__DIR__ . '/../resources/templates', [
       'cache' => __DIR__ . '/../tmp/cache'
   ]);
});

My action class has this

public function __invoke(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
{
    $view = Twig::fromRequest($request);
    return $view->render($response, 'login.twig');
}

I get this error message when trying to access the page.

 {"error":{"message":"Twig could not be found in the server request attributes using the key \"view\".","code":0,"file":"/var/www/html/client_acct_mgr/vendor/slim/twig-view/src/Twig.php","line":74,"previous":null,"trace":

In the bootstrap file, the set command should have set a key in the container which was called in line 10 of the bootstrap file.

However, I know that the key is being injected into the container because if I remove set from the bootstrap. I get an error on the middleware.

    $app->add(TwigMiddleware::createFromContainer($app));

If the middleware can find the key, why can’t the LoginAction class find the same key?
Thanks in advance.

There are two different “keys”, the DI container key (default is “view”) and the server request attribute key, default is also “view”. In your specific case, the server request attribute “view” is missing.

This can happen if the TwigMiddleware is not in the correct position (order) or if another middleware creates a new request object without this attribute.

1 Like

This is my middleware stack.

return function (App $app) {
    $app->addBodyParsingMiddleware();
    $app->add(ValidationExceptionMiddleware::class);
    $app->addRoutingMiddleware();
    $app->add(TwigMiddleware::createFromContainer($app));
    $app->add(BasePathMiddleware::class);
    $app->add(ErrorMiddleware::class);
};

Where should twig go in this stack?

The TwigMiddleware should be added before the RoutingMiddleware.

Is there any documentation that shows how to properly add the Twig to the Slim4 skeleton?
Moving it before the routing on changed the error message. Thanks for the suggestion.

{"error":{"message":"Twig could not be found in the server request attributes using the key \"view\".","code":0,"file":"/var/www/html/client_acct_mgr/vendor/slim/twig-view/src/Twig.php","line":74,"previous":null,"trace":[{"file":"/var/www/html/client_acct_mgr/src/Action/Login/LoginAction.php","line":21,"function":"fromRequest","class":"Slim\\Views\\Twig","type":"::"},{"file":"/var/www/html/client_acct_mgr/vendor/slim/slim/Slim/Handlers/Strategies/RequestResponse.php","line":43,"function":"__invoke","class":"App\\Action\\Login\\LoginAction","type":"->"},{"file":"/var/www/html/client_acct_mgr/vendor/slim/slim/Slim/Routing/Route.php","line":384,"function":"__invoke","class":"Slim\\Handlers\\Strategies\\RequestResponse","type":"->"},{"file":"/var/www/html/client_acct_mgr/vendor/slim/slim/Slim/MiddlewareDispatcher.php","line":81,"function":"handle","class":"Slim\\Routing\\Route","type":"->"},{"file":"/var/www/html/client_acct_mgr/vendor/slim/slim/Slim/MiddlewareDispatcher.php","line":81,"function":"handle","class":"Slim\\MiddlewareDispatcher","type":"->"},{"file":"/var/www/html/client_acct_mgr/vendor/slim/slim/Slim/Routing/Route.php","line":341,"function":"handle","class":"Slim\\MiddlewareDispatcher","type":"->"},{"file":"/var/www/html/client_acct_mgr/vendor/slim/slim/Slim/Routing/RouteRunner.php","line":84,"function":"run","class":"Slim\\Routing\\Route","type":"->"},{"file":"/var/www/html/client_acct_mgr/vendor/slim/slim/Slim/Middleware/BodyParsingMiddleware.php","line":68,"function":"handle","class":"Slim\\Routing\\RouteRunner","type":"->"},{"file":"/var/www/html/client_acct_mgr/vendor/slim/slim/Slim/MiddlewareDispatcher.php","line":147,"function":"process","class":"Slim\\Middleware\\BodyParsingMiddleware","type":"->"},{"file":"/var/www/html/client_acct_mgr/src/Middleware/ValidationExceptionMiddleware.php","line":27,"function":"handle","class":"Psr\\Http\\Server\\RequestHandlerInterface@anonymous\u0000/var/www/html/client_acct_mgr/vendor/slim/slim/Slim/MiddlewareDispatcher.php:128$22","type":"->"},{"file":"/var/www/html/client_acct_mgr/vendor/slim/slim/Slim/MiddlewareDispatcher.php","line":209,"function":"process","class":"App\\Middleware\\ValidationExceptionMiddleware","type":"->"},{"file":"/var/www/html/client_acct_mgr/vendor/slim/twig-view/src/TwigMiddleware.php","line":115,"function":"handle","class":"Psr\\Http\\Server\\RequestHandlerInterface@anonymous\u0000/var/www/html/client_acct_mgr/vendor/slim/slim/Slim/MiddlewareDispatcher.php:167$23","type":"->"},{"file":"/var/www/html/client_acct_mgr/vendor/slim/slim/Slim/MiddlewareDispatcher.php","line":147,"function":"process","class":"Slim\\Views\\TwigMiddleware","type":"->"},{"file":"/var/www/html/client_acct_mgr/vendor/slim/slim/Slim/Middleware/RoutingMiddleware.php","line":59,"function":"handle","class":"Psr\\Http\\Server\\RequestHandlerInterface@anonymous\u0000/var/www/html/client_acct_mgr/vendor/slim/slim/Slim/MiddlewareDispatcher.php:128$22","type":"->"},{"file":"/var/www/html/client_acct_mgr/vendor/slim/slim/Slim/MiddlewareDispatcher.php","line":147,"function":"process","class":"Slim\\Middleware\\RoutingMiddleware","type":"->"},{"file":"/var/www/html/client_acct_mgr/vendor/selective/basepath/src/BasePathMiddleware.php","line":52,"function":"handle","class":"Psr\\Http\\Server\\RequestHandlerInterface@anonymous\u0000/var/www/html/client_acct_mgr/vendor/slim/slim/Slim/MiddlewareDispatcher.php:128$22","type":"->"},{"file":"/var/www/html/client_acct_mgr/vendor/slim/slim/Slim/MiddlewareDispatcher.php","line":209,"function":"process","class":"Selective\\BasePath\\BasePathMiddleware","type":"->"},{"file":"/var/www/html/client_acct_mgr/vendor/slim/slim/Slim/Middleware/ErrorMiddleware.php","line":107,"function":"handle","class":"Psr\\Http\\Server\\RequestHandlerInterface@anonymous\u0000/var/www/html/client_acct_mgr/vendor/slim/slim/Slim/MiddlewareDispatcher.php:167$23","type":"->"},{"file":"/var/www/html/client_acct_mgr/vendor/slim/slim/Slim/MiddlewareDispatcher.php","line":209,"function":"process","class":"Slim\\Middleware\\ErrorMiddleware","type":"->"},{"file":"/var/www/html/client_acct_mgr/vendor/slim/slim/Slim/MiddlewareDispatcher.php","line":81,"function":"handle","class":"Psr\\Http\\Server\\RequestHandlerInterface@anonymous\u0000/var/www/html/client_acct_mgr/vendor/slim/slim/Slim/MiddlewareDispatcher.php:167$23","type":"->"},{"file":"/var/www/html/client_acct_mgr/vendor/slim/slim/Slim/App.php","line":215,"function":"handle","class":"Slim\\MiddlewareDispatcher","type":"->"},{"file":"/var/www/html/client_acct_mgr/vendor/slim/slim/Slim/App.php","line":199,"function":"handle","class":"Slim\\App","type":"->"},{"file":"/var/www/html/client_acct_mgr/public/index.php","line":3,"function":"run","class":"Slim\\App","type":"->"}]}}

You could take a look into the documentation. In my Slim 4 eBook you can find a working example of how to integrate this packing into any Slim project.

This was a laugh out loud moment for me because I purchased the book earlier this year. It has been sitting in my download folder all this time. I unzipped the book and browsed to the twig section of the book. I copy pasted all the code making the necessary changes. Uploaded to the server and bam! Couldn’t create the cache. Changed the folder permissions then the template came up!!!

Happier than a dead pig in the sunshine!

Thanks odan, your book rocks!

Am I using too many !!!

1 Like