cakephpcakephp-3.2

how to access parameters from url in cakephp 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 ?


Solution

  • 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');