Reputation: 11
I'm pretty new to using slim framework with twig-view. I am seeing this error when I try to render view from slim container, this is the code that returns an error page.
<?php
$app->get('/home', function($request, $response){
return $this->$view->render($response, 'home.twig');
});
?>
The above code returns this errorSlim application error, but when I return a simple string like the example below:
<?php
$app->get('/home', function($request, $response){
return "Hello World!";
});
?>
It outputs Hello World! correctly on the browser, but when I try this:
<?php
$app->get('/home', function($request, $response){
return $this->$view->render($response, 'home.twig');
});
?>
It returns the error Slim application error. The below code is my index page.
<?php
session_start();
require __DIR__ . '/../vendor/autoload.php';
$app = new \Slim\App([
'settings' => [
'displayErrorDetails' => true,
]
]);
$container = $app->getContainer();
$container['view'] = function($container){
$view = new \Slim\Views\Twig(__DIR__ . '/../resources/views/home.twig', [
'cache' => false,
]);
$view->addExtension(new Slim/Views/TwigExtension(
$container->router,
$container->request->getUri()
));
return $view;
};
require __DIR__ . '/../app/route.php';
?>
On
What am I not doing correctly?
Upvotes: 1
Views: 1039
Reputation: 33
Make sure your .htaccess file exits in the root folder with:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]
RewriteRule .* - [env=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
Upvotes: 0
Reputation: 123
You should define your views folder, not your file.
$container['view'] = function ($container) {
$view = new \Slim\Views\Twig(__DIR__ . '/../resources/views', [
'cache' => false
]);
$view->addExtension(new \Slim\Views\TwigExtension(
$container->router,
$container->request->getUri()
));
return $view;
}
And in your route
you can do this
$app->get('/', function ($request, $response) {
return $this->container->view->render($response, 'home.twig');
});
Notice that home.twig must be inside views folder.
Upvotes: 0
Reputation: 688
The problem is that you are trying to access $this->$view
but the container only knows about $this->view
.
Upvotes: 1