3

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 ?

4
  • 2
    are you sure you are using cake3? cake 3 would return /posts/view/?foo=bar. Seems a cake2 application Commented Aug 4, 2016 at 6:46
  • 1
    $var = $this->request->param('foo') Commented Aug 4, 2016 at 6:52
  • @arilia check this http://book.cakephp.org/3.0/en/views/helpers/url.html#generating-urls Commented Aug 4, 2016 at 6: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 Commented Aug 4, 2016 at 6:59

3 Answers 3

10

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');
Sign up to request clarification or add additional context in comments.

Comments

1

CakePHP 3.* version can use request Query :

$this->request->getQuery('utm_source')

Comments

0

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.