php 我可以使用 twig 模板引擎组件显示参数值 ($_POST, $_GET, $_SESSION, $_SESSION)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19554429/
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
can I display parameters value ($_POST, $_GET, $_SESSION, $_SESSION) using twig template engine component
提问by antoniputra
I had tried do like this :
我试过这样做:
{{ $_GET['page'] }}
but that still didn't work.
但这仍然不起作用。
采纳答案by Maerlyn
The only variables available in Twig are:
Twig 中唯一可用的变量是:
- the ones you pass through the second parameter of Twig_Environment#render()
- the ones you pass by calling Twig_Environment#addGlobal()
- 你通过Twig_Environment#render()的第二个参数传递的那些
- 您通过调用Twig_Environment#addGlobal()传递的那些
If you wish to have a pagevariable available, add "page" => $_GET["page"]to the second parameter of render.
如果您希望有一个page可用的变量,请添加"page" => $_GET["page"]到 的第二个参数render。
If you wish to have the complete $_GETsuperglobal available, add "GET" => $_GETto the second parameter of render.
如果您希望获得完整的$_GET超全局变量,请添加"GET" => $_GET到render.
回答by Fouad Fodail
For $_POST variables use this :
对于 $_POST 变量使用这个:
{{ app.request.parameter.get("page") }}
For $_GET variables use this :
对于 $_GET 变量使用这个:
{{ app.request.query.get("page") }}
For $_COOKIE variables use this :
对于 $_COOKIE 变量使用这个:
{{ app.request.cookies.get("page") }}
For $_SESSION variables use this :
对于 $_SESSION 变量使用这个:
{{ app.request.session.get("page") }}
回答by sadiq
Another working solution @ 2019
另一个工作解决方案@ 2019
app.request.get('page')
app.request.request.get('page')
回答by Matthew Cawley
You cannot access raw PHP values in TWIG so you must attached anything that you need to the twig object.
您无法在 TWIG 中访问原始 PHP 值,因此您必须将所需的任何内容附加到树枝对象。
Here is an example of attaching the $_GET, $_POSTand $_SESSIONto twig.
这是将$_GET,$_POST和附加$_SESSION到树枝的示例。
//initialise your twig object
$loader = new \Twig_Loader_Filesystem(__PATH_TO_TEMPLATES_FOLDER__');
$this->twig = new \Twig_Environment($loader);
$this->twig->addGlobal('_session', $_SESSION);
$this->twig->addGlobal('_post', $_POST);
$this->twig->addGlobal('_get', $_GET);
Now lets assume you have a value called usernamestored in each of the above ($_GET, $_POSTand $_SESSION)
You can now access all of these inside your template as follows.
现在让我们假设您username在上面的每个 ( $_GET,$_POST和$_SESSION) 中都有一个名为存储的值,您现在可以访问模板中的所有这些,如下所示。
{{ _session.username }}
{{ _post.username }}
{{ _get.username }}
Hope this helps
希望这可以帮助

