2

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 ?

Anuj TBE
  • 9,198
  • 27
  • 136
  • 285
  • 2
    are you sure you are using cake3? cake 3 would return `/posts/view/?foo=bar`. Seems a cake2 application – arilia Aug 04 '16 at 06:46
  • 1
    `$var = $this->request->param('foo')` – Jacek B Budzynski Aug 04 '16 at 06:52
  • @arilia check this [http://book.cakephp.org/3.0/en/views/helpers/url.html#generating-urls](http://book.cakephp.org/3.0/en/views/helpers/url.html#generating-urls) – Anuj TBE Aug 04 '16 at 06:53
  • I guess is an error in the documentation or maybe the recently changed it. Did you try your code? I just tried your exact code in a cake3.2.13 application and it gives `/posts/view/?foo=bar` as expected. Anyway @JacekBBudzyñski comment is correct – arilia Aug 04 '16 at 06:59

3 Answers3

9

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');
arilia
  • 9,373
  • 2
  • 20
  • 44
1

CakePHP 3.* version can use request Query :

$this->request->getQuery('utm_source')
Irshad Khan
  • 5,670
  • 2
  • 44
  • 39
0

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

$params = $this->request->getQueryParams();
gdm
  • 7,647
  • 3
  • 41
  • 71