问题
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