BadHorsie
BadHorsie

Reputation: 14554

CakePHP 3 - Configure route to allow optional parameter

I have a route like this:

$routes->connect('/custom/url', [
    'prefix' => 'admin', 'controller' => 'Things', 'action' => 'index'
]);

I want to allow an optional passed parameter so the URL can be /custom/url/123 but also still allow it to not have the parameter at all, like /custom/url.

If I change the route to /custom/url/:param it throws an exception if I visit the URL without the extra parameter. How can I make the parameter matching lazy?

Upvotes: 4

Views: 955

Answers (1)

Salines
Salines

Reputation: 5767

routes like:

$routes->connect('/custom/url/*', [
    'prefix' => 'admin', 'controller' => 'Things', 'action' => 'index'
]);

In controller

public function index($param = null){
  // your code here
}

Upvotes: 4

Related Questions