Silex - Twig_Error_Syntax:函数“路径";不存在
根据 Silex 文档:p><块引用>
Symfony 提供了一个 Twig 桥,它提供了一些 Symfony2 组件和 Twig 之间的额外集成.将其作为依赖项添加到您的 composer.json 文件中.
我在 composer.json
文件中包含以下内容:
<代码>{要求": {"silex/silex": "1.*","twig/twig": ">=1.8,<2.0-dev",symfony/树枝桥":2.3.*"}}
我像这样注册 TwigServiceProvider()
:
$app->register(new SilexProviderTwigServiceProvider(), array('twig.path' =>__DIR__ .'/意见'));
我正在尝试像这样使用 twig path()
方法:
<a href="{{ path('logout') }}">注销</a>
我得到的错误如下:
<块引用>Twig_Error_Syntax: 函数路径"不存在
为什么会出现此错误?
- 我已尝试切换版本以检查是否是版本问题
- 一个谷歌群组评论建议注册"树枝桥提供者,但这不存在
- 我不想在我的所有模板中使用:
app.url_generator.generate
我找到的一个临时解决方案:
确保 UrlGeneratorServiceProvider()
已注册:
$app->register(new UrlGeneratorServiceProvider());
为 path()
为 twig 创建一个新函数:
$app['twig']->addFunction(new Twig_SimpleFunction('path', function($url) use ($app) {返回 $app['url_generator']->generate($url);}));
我不应该这样做!我怎样才能让这个工作正常?
解决方案希望这将有助于未来的观众,因为许多人在没有可靠答案的情况下发布了这个问题,所以这里有一个.
你需要UrlGeneratorServiceProvider()
注册
$app->register(new UrlGeneratorServiceProvider());
另外,正如umpirsky 在评论中提到的,您需要通过composer 安装symfony/twig-bridge
.
您不需要添加自己的函数.在加载树枝模板之前,您需要同时注册 TwigServiceProvider()
和 UrlGeneratorServiceProvider()
.这在文档中并不容易看出.
According to the Silex documentation:
Symfony provides a Twig bridge that provides additional integration between some Symfony2 components and Twig. Add it as a dependency to your composer.json file.
I include the following in my composer.json
file:
{
"require": {
"silex/silex": "1.*",
"twig/twig": ">=1.8,<2.0-dev",
"symfony/twig-bridge": "2.3.*"
}
}
I register the TwigServiceProvider()
like so:
$app->register(new SilexProviderTwigServiceProvider(), array(
'twig.path' => __DIR__ . '/views'
));
I'm attempting to use the twig path()
method like so:
<a href="{{ path('logout') }}">Log out</a>
The error I get is as follows:
Twig_Error_Syntax: The function "path" does not exist
Why am I getting this error?
- I have tried switching around versions to check if it is a version issue
- One google groups comment suggested 'registering' the twig bridge provider, but this doesn't exist
- I don't want to have to use:
app.url_generator.generate
in all my templates instead
A temporary solution I have found:
Ensure The UrlGeneratorServiceProvider()
is registered:
$app->register(new UrlGeneratorServiceProvider());
Create a new function for twig for path()
:
$app['twig']->addFunction(new Twig_SimpleFunction('path', function($url) use ($app) {
return $app['url_generator']->generate($url);
}));
I shouldn't have to do this!! How can I get this working properly?
解决方案Hopefully this will help future viewers as many have posted this question without a solid answer, so here is one.
It is literally that you need UrlGeneratorServiceProvider()
registered
$app->register(new UrlGeneratorServiceProvider());
Also, as umpirsky mentions in the comments, you need symfony/twig-bridge
installed via composer.
You do not need to add your own function. You need both the TwigServiceProvider()
and the UrlGeneratorServiceProvider()
registered before loading your twig template. This isn't easily apparent from the documentation.
相关文章