CakePHP 3 - Configure route to allow optional parameter

风格不统一 提交于 2019-12-12 16:30:58

问题


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?


回答1:


routes like:

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

In controller

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


来源:https://stackoverflow.com/questions/37448765/cakephp-3-configure-route-to-allow-optional-parameter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!