CakePHP 3-配置路由以允许可选参数 [英] CakePHP 3 - Configure route to allow optional parameter

查看:52
本文介绍了CakePHP 3-配置路由以允许可选参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样一条路线:

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

我想允许一个可选的传递参数,以便URL可以是 / custom / url / 123 ,但仍然允许它根本没有参数,例如 / custom / url

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.

如果我将路由更改为 / custom / url /:param ,则如果我访问没有附加参数的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?

推荐答案

路线,例如:

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

在控制器中

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

这篇关于CakePHP 3-配置路由以允许可选参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆