Silex v1.3到Silex v2:Twig_Error_Runtime

Mac*_*nie 3 php templates symfony twig silex

我最近将我的Silex v1.3项目迁移到了Silex v2,其中一切正常.我得到一个枝条错误,我不明白原因

Twig_Error_Runtime in Template.php line 590:

Neither the property "request" nor one of the methods "request()", "getrequest()"/"isrequest()" or "__call()" exist and have public access in class "Silex\Application" in "index.twig" at line 5
Run Code Online (Sandbox Code Playgroud)

这是我的index.twig文件的第5行

{% extends "layout.twig" %}


{% block stylsheets %}
<link rel="stylesheet" type="text/css" href="{{ app.request.basepath }}/public/libs/odometer/themes/odometer-theme-minimal.css" />
Run Code Online (Sandbox Code Playgroud)

当然在布局中,我称之为

{% block stylsheets %}{% endblock %}
Run Code Online (Sandbox Code Playgroud)

但我完全不确定,问题出现在twig文件上......是否有必要改变?

这是控制器的摘录:

$app->get('/', function () use ($app) {

    return $app['twig']->render('index.twig', [
        ...
    ]);
})->bind('homepage');
Run Code Online (Sandbox Code Playgroud)

感谢帮助

seb*_*cas 5

我发现app.request.uri最近Twig不再使用了.

但如果你使用global.request.uri它工作正常.

我在Twig的文档中没有找到任何关于它的信息.


Joh*_*e66 5

正如 seblucas 所说,global.request.uri工作正常。但是我找到了另一个解决方案来解决这个问题。您可以替换app.request.uriapp.request_stack.currentrequest.basepath. 我不知道什么是最好的解决方案...