Error 500 when using Slim Framework with Twig

1

I'm developing a simple website , with Slim Framework and Twig for engine , only returning error in the browser, this is the content of my file index.php (in the project root):

require_once './vendor/autoload.php';

// Create container
$container = new \Slim\Container;

// Register component on container
$container['view'] = function ($c) {
    $view = new \Slim\Views\Twig('app/views', [
        'cache' => 'app/storage/cache'
    ]);
    $view->addExtension(new \Slim\Views\TwigExtension(
        $c['router'],
        $c['request']->getUri()
    ));

    return $view;
};

// Create app
$app = new \Slim\App($container);

// Routes
$app->get('/', function() use ($app) {
    $app->render('index.twig', ['app' => $app]);
})->name('home');

// Run app
$app->run();

My folder structure looks like this:

app
  |__views
  |__storage
           |__cache
vendor
     |__twig
     |__slim
     |__psr
     |__composer
assets
     |__css
     |__js
     |__img
     |__fonts

I followed the instructions in this example: h ttp: //www.slimframework.com/docs/features/templates. html

I would like to render the view "home", but only returns:

  

Error 500

    
asked by anonymous 26.11.2015 / 15:26

1 answer

0

At the beginning of your code it looks like you're using the Slim Framework v3, but by the way you created the route , there is the shape of v2.

I'll assume you're using Slim v3.

As without .htaccess, first create it:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]

If you are not using apache you can find the codes for other webservers at link

Change your route code to:

// Routes
$app->get( '/', function ( $request, $response, $args ) {
    return $this->view->render( $response, 'index.twig', [ 'app', $this ] );
} )->setName( 'home' );

There have been considerable changes between Slim v2 and Slim v3, highlighted the main ones in the documentation at link

Complement

To enable debug in Slim v3, add container

$container['settings']['displayErrorDetails'] = true;

I'm still studying this new version a bit, but I hope I have helped.

    
26.12.2015 / 15:03