how to access parameters from url in cakephp 3

混江龙づ霸主 提交于 2020-07-03 08:53:30

问题


In cook book of cakephp 3. It is given to build url using

echo $this->Url->build([
    "controller" => "Posts",
    "action" => "view",
    "foo" => "bar"
]);

which will output as

/posts/view/foo:bar

How to access the foo:bar in action and save in a variable $foo ?


回答1:


there's an error in the cookbook, so I opened this ticket

if you use this code

echo $this->Url->build([
    "controller" => "Posts",
    "action" => "view",
    "foo" => "bar"
]);

you'll get an url like this

/posts/view/?foo=bar

the manual here explains how to access the GET parameters

you can do

$this->request->query('foo');

or

 $this->request->query['foo'];

the first is null safe, it means that if the 'foo' parameter is not set you simply get null and not an error

Edit

after 3.4.0 the new syntax is

$this->request->getQuery('foo');



回答2:


Or in one line to get all the params as an Array:

$params = $this->request->getQueryParams();


来源:https://stackoverflow.com/questions/38760117/how-to-access-parameters-from-url-in-cakephp-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!