Closed EdwinHuijsing closed 4 years ago
So the problem comes from using TwigMiddleware::createFromContainer()
in this instance the middleware gets created but the default $attributeName
is null
meaning that it will not be appending it to the request.
https://github.com/slimphp/Twig-View/blob/3.x/src/TwigMiddleware.php#L68
You are trying to access twig via Twig::fromRequest()
within your route callback which is looking for the attribute on the request.
@adriansuter do you remember why we instantiated the middleware that way with a null attributeName
in the TwigMiddleware::fromContainer()
method?
@EdwinHuijsing
In the interim you can simply access it directly via the container from your route callback using:
$twig = $this->get('view')
I guess the idea was, that if a user explicitly injects the view to the controllers, then there is no need to add the instance to the request object (as attribute). In that case, the user can simply pass null
as attribute name.
See also #127
@adriansuter makes sense. Why include the object twice in both route and container. Pick one or the other.
Also worth mentioning that the examples on the README are pretty clear:
Example with container:
// Create App
$app = AppFactory::create();
// Add Twig-View Middleware
$app->add(TwigMiddleware::createFromContainer($app));
// Define named route
$app->get('/hello/{name}', function ($request, $response, $args) {
return $this->get('view')->render($response, 'profile.html', [
'name' => $args['name']
]);
})->setName('profile');
Example without container:
// Add Twig-View Middleware
$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', [
'name' => $args['name']
]);
})->setName('profile');
I'm closing this as resolved.
maybe need more documentation in wiki with the skeleton template
I installed the slim skeleton app like
composer create-project slim/slim-skeleton [my-app-name]
then I addedcomposer require zeuxisoo/slim-whoops
. and lastcomposer require slim/twig-view:^3.0
.To get twig up and running with php-di (autowire) I added/changed the code blow. While executing the code I get the error:
Twig could not be found in the server request attributes using the key "view"
public/index.php
app/settings.php add
app/dependencies.php add
app/middleware.php add
app/routes.php
app/templates/index.twig
I am able to fix this, but is more of a hack in the twig-view package in
Slim\Views\TwigMiddleware
I alter the methodecreateFromContainer
Is this a bug or am I doing something wrong?