How to access the last part of a GET url in CakePHP 3

Viewed 73

I'm building my link like this:

$this->Html->link(
    $this->Icon->fa('plus'),
    ['controller' => 'Questions', 'action' => 'add', $questionnaire->id],
    [
        'title' => 'Add'
    ]
);

This creates the desired icon on my browser, with this link:

<host>/questionnaire/questions/add/2

On the "other side", by using for example

$this->request->getParam('action')

I can correctly get "add" as a value.

But how can I access the 2, so the last part of the url?

I've tried with

$this->request->getQueryParams()

and it returns an empty array.

1 Answers

That depends on your connected routes. If there is a corresponding route that has a route element defined for that value, eg

/questionnaire/questions/add/:id

then you could get the value via that element name, getParam('id').

If the corresponding route does not have a dedicated named element for that value, but instead a *, which is the default for fallback routes connected via fallbacks(), then it will land in the pass parameter, getParam('pass'), which is an array of trailing values, and your value would sit at index 0.

See also

Related