简体   繁体   中英

Error using Slim framework and Twig template

I'm trying to make Slim work with Twig template system, this is part of my index.php

// Twig [Template]
require 'Extras/Views/Twig.php';
TwigView::$twigDirectory = __DIR__ . '/vendor/Twig/lib/Twig/';

//Slim
require 'Slim/Slim.php';
\Slim\Slim::registerAutoloader();

$app = new \Slim\Slim(array(
    'view' => $twigView
));

And this is my structure

Extras
    |_Views
        |_Twig.php
Slim
templates
vendor
    |_Twig
        |_lib
            |_Twig
index.php

I try several times with other configurations and searching buy I ALLWAYS get this error:

Fatal error: Class 'Slim\View' not found in C:\wamp\www\slim\Extras\Views\Twig.php on line 43

Can anyone help me here? All the examples I had found was using composer

Ok, I solve it. This is the solution:

// Slim PHP
require "Slim/Slim.php";
\Slim\Slim::registerAutoloader();

// Twig
require "Twig/lib/Twig/Autoloader.php";
Twig_Autoloader::register();

// Start Slim.
/** @var $app Slim */
$app = new \Slim\Slim(array(
    "view" => new \Slim\Extras\Views\Twig()
));

And this is my structure now.

Slim
|_Extras
    |_Views
        |_Twig.php
|_Slim
templates
Twig
 |_lib
   |_Twig
      |_Autoloader.php
index.php

¡I hope this help someone else!

Now Slim-Extras is DEPRECATED, we must use Slim-Views ( https://github.com/codeguy/Slim-Views ):

require "Slim/Slim.php";
\Slim\Slim::registerAutoloader();

$slim = new \Slim\Slim( array(
        'debug' => false, 
        'templates.path' => 'fooDirTemplates', 
        'view' => '\Slim\Views\Twig'
    ));

$twigView = $slim->view();
$twigView->parserOptions = array(
        'debug' => false
    );
$twigView->parserDirectory = 'Twig';
$twigView->parserExtensions = array(
        '\Slim\Views\TwigExtension'
    );

$slim->notFound( 'fooNotFoundFunction' );
$slim->error( 'fooErrorFunction' );

// SLIM routes...

$slim->run();

If someone is still running in to this issue. The problem for me was that I had installed both slim/views AND slim/twig-view. I uninstalled slim/views and it worked

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM