如何从cakephp 3中的URL访问参数 [英] how to access parameters from url in cakephp 3
本文介绍了如何从cakephp 3中的URL访问参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在cakephp 3的烹饪书中。使用
In cook book of cakephp 3. It is given to build url using
echo $this->Url->build([
"controller" => "Posts",
"action" => "view",
"foo" => "bar"
]);
将输出为
/posts/view/foo:bar
如何访问 foo:bar
并保存在变量 $ foo
吗?
How to access the foo:bar
in action and save in a variable $foo
?
推荐答案
食谱中存在错误,因此我打开了 此 票
there's an error in the cookbook, so I opened this ticket
如果使用此代码
echo $this->Url->build([
"controller" => "Posts",
"action" => "view",
"foo" => "bar"
]);
您将获得这样的网址
/posts/view/?foo=bar
手册 此处 说明如何访问GET参数
the manual here explains how to access the GET parameters
您可以执行
$this->request->query('foo');
或
$this->request->query['foo'];
第一个是null安全的,这意味着如果' foo 没有设置code>'参数,您只会得到
null
而不是错误
the first is null safe, it means that if the 'foo
' parameter is not set you simply get null
and not an error
编辑
在3.4.0之后,新语法为
after 3.4.0 the new syntax is
$this->request->getQuery('foo');
这篇关于如何从cakephp 3中的URL访问参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文