php Twig 模板引擎:获取当前 url
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9339370/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Twig templates engine: get current url
提问by nKognito
How can I get the current URL from a Twig template?
如何从 Twig 模板获取当前 URL?
I am using Twig with PHP, without any other framework.
我在 PHP 中使用 Twig,没有任何其他框架。
采纳答案by erisco
Finding the current URL
查找当前 URL
The current URL is supplied by your web server and written to the $_SERVER
super-global. Run this small script, <?php echo '<pre>'; print_r($_SERVER);
, through your server and root around to find the value(s) you are looking for.
当前 URL 由您的 Web 服务器提供并写入$_SERVER
超级全局。运行这个小脚本,<?php echo '<pre>'; print_r($_SERVER);
通过你的服务器和 root 找到你正在寻找的值。
Related questions on this subject:
关于这个主题的相关问题:
The PHP manual describes the nature of the available $_SERVER
values here.
Getting the URL in TWIG
获取 TWIG 中的 URL
After you have the URL, you need to pass it as a template variable when calling render(...)
on the Twig template instance. For example, you might code this.
获得 URL 后,您需要在调用render(...)
Twig 模板实例时将其作为模板变量传递。例如,您可以对此进行编码。
$current_url = // figure out what the current url is
// pass the current URL as a variable to the template
echo $template->render(array('current_url' => $current_url));
To use the variable in the template, you use the {{ variable_name }}
syntax.
要在模板中使用变量,请使用{{ variable_name }}
语法。
回答by neemzy
The following works in Silex and most certainly in Symfony2 as they share the Request class (I did not test though) :
以下在 Silex 中有效,当然在 Symfony2 中也有效,因为它们共享 Request 类(虽然我没有测试):
{{ app.request.getRequestUri() }}
回答by Breith
Go http://api.symfony.com/2.3/Symfony/Component/HttpFoundation/Request.html
去http://api.symfony.com/2.3/Symfony/Component/HttpFoundation/Request.html
or : {{ app.request.getUri() }}
for full Uri.
或 :{{ app.request.getUri() }}
对于完整的 Uri。
回答by Pixadelic
Keeping best practice in mind, at this time you should use Symfony\Component\HttpFoundation\RequestStack
.
牢记最佳实践,此时您应该使用Symfony\Component\HttpFoundation\RequestStack
.
See http://symfony.com/blog/new-in-symfony-2-4-the-request-stack.
请参阅http://symfony.com/blog/new-in-symfony-2-4-the-request-stack。
As of Symfony 2.4, the best practice is to never inject the request service, but to inject the request_stack service instead [...]
从 Symfony 2.4 开始,最佳实践是永远不要注入请求服务,而是注入 request_stack 服务 [...]
So in a Silex application it could be achieved with :
因此,在 Silex 应用程序中,可以通过以下方式实现:
app.request_stack.getCurrentRequest.getUri
回答by Benjamin
Here something I found to make it generic with the sliex Framework. I guess my solution is not perfect but it's get the job done.
在这里,我发现可以使用 sliex 框架使其通用。我想我的解决方案并不完美,但它完成了工作。
in your PHP code add this code :
在您的 PHP 代码中添加以下代码:
$app = new Silex\Application();
// add the current url to the app object.
$app['current_url'] = $_SERVER['REQUEST_URI'];
Then in your Twig template you can do
然后在你的 Twig 模板中你可以做
{{ app.current_url }}
Let me know what is the botom line of this method.
让我知道这种方法的底线是什么。