标识符" twig"未定义 - Silex Framework

时间:2014-07-25 10:24:46

标签: php twig silex

感谢您的关注,

我已创建此APP:

// web/index.php
require_once __DIR__.'/../vendor/autoload.php';

use Silex\Application;
use Silex\Provider\TwigServiceProvider;

$app = new Application();
$app['debug'] = true;

$app->register(new TwigServiceProvider(), array(
    'twig.path' => __DIR__.'/templates',
    'twig.class_path' => __DIR__.'/../vendor/twig/twig/lib',
    'twig.options' => array('cache' => __DIR__.'/../cache'),
));

$app->get('/', function () use ($app) {
    return $app['twig']->render('base.html.twig', array());
});

return $app;

但是,当我加载/加载base.html.twig的实例时,会出现此错误:

InvalidArgumentException: Identifier "twig" is not defined.
in /home/victor/workspace/testProject/vendor/pimple/pimple/lib/Pimple.php line 78
at Pimple->offsetGet('twig') in /home/victor/workspace/testProject/src/app.php line 41
at {closure}()
at call_user_func_array(object(Closure), array()) in /home/victor/workspace/testProject/vendor/symfony/http-kernel/Symfony/Component/HttpKernel/HttpKernel.php line 145
at HttpKernel->handleRaw(object(Request), '1') in /home/victor/workspace/testProject/vendor/symfony/http-kernel/Symfony/Component/HttpKernel/HttpKernel.php line 66
at HttpKernel->handle(object(Request), '1', true) in /home/victor/workspace/testProject/vendor/silex/silex/src/Silex/Application.php line 538
at Application->handle(object(Request)) in /home/victor/workspace/testProject/vendor/silex/silex/src/Silex/Application.php line 515
at Application->run() in /home/victor/workspace/testProject/web/index.php line 11

我已经按照官方教程,使用composer安装它并双重检查了所有路径......

1 个答案:

答案 0 :(得分:2)

尝试删除" twig.class_path"条目。

我喜欢这样,它有效:

    $options = [
        'twig.path' => ...,
        'twig.options' => [
            'strict_variables'  => false
        ]
    ];

    $this->_app->register(new TwigServiceProvider(), $options);