Error 500 when using Slim Framework with Twig

Asked

Viewed 224 times

1

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

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 is like this:

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

I followed the instructions in this example:ttp://www.slimframework.com/Docs/Features/templates.html

I’d like to render the view "home", but just returns:

Error 500

  • In the main file put, ini_set('display_errors', true); error_reporting(E_ALL);

  • 1

    I checked the apache log and this written this: Class 'Slim\\Container' not found

  • How is your . htaccess file? Error may be in it.

  • is without . htaccess

1 answer

0

At the beginning of your code it seems that you are using the Slim Framework v3, but by the way you created the route, there is the form of v2.

I’ll assume you’re using Slim v3.

As it is 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 others webservers in http://www.slimframework.com/docs/start/web-servers.html

Change your route code to:

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

There were considerable changes between the Slim v2 and the Slim v3, highlighted the main ones in the documentation on http://www.slimframework.com/docs/start/upgrade.html

Complement

To enable the debug in Slim v3, add to container

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

I’m still studying a little bit this new version, but I hope I’ve helped.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.