2

在 cakephp 3 的烹饪书中。给出了使用构建 url

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

这将输出为

/posts/view/foo:bar

如何访问实际foo:bar操作并保存在变量中$foo

4

3 回答 3

9

食谱中有错误,所以我打开了这张

如果您使用此代码

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

你会得到一个这样的网址

/posts/view/?foo=bar

此处的手册解释了如何访问 GET 参数

你可以做

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

或者

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

第一个是空安全的,这意味着如果foo未设置“”参数,您只需获取null而不是错误

编辑

3.4.0 之后的新语法是

$this->request->getQuery('foo');
于 2016-08-05T07:08:36.423 回答
0

或者在一行中将所有参数作为数组获取:

$params = $this->request->getQueryParams();
于 2020-02-01T17:03:39.207 回答
0

CakePHP 3.* 版本可以使用 request Query :

$this->request->getQuery('utm_source')
于 2020-12-07T08:20:39.080 回答