Cakephp 如果路由以某个字符串/字符开头,请更改路由

Cakephp 如果路由以某个字符串/字符开头,请更改路由,cakephp,Cakephp,我在routes.php中有这个: $builder ->connect( '/a/*', [ "controller" => "Api", "action" => "foo" ] ) ->setHost("domain.world"); 这非常适合像这样的URLhttps://domain.world/a/5nxdy9lmcthc。当我想根据URL中的第

我在
routes.php中有这个:

$builder
    ->connect( '/a/*', [ "controller" => "Api", "action" => "foo" ] )
    ->setHost("domain.world");
这非常适合像这样的URL
https://domain.world/a/5nxdy9lmcthc
。当我想根据URL中的第一个字符启用不同的路由时,我失败了

例如,这些不同的行动目标:

  • a*
    -->
    a动作
  • b*
    -->
    b动作
  • c*
    -->
    cbAction
它现在与
/
一起工作,但出于某些原因,我想省略
/
,因此URL将是
https://domain.world/a5nxdy9lmcthc
https://domain.world/b5nxdy9lmcthc
这将触发不同的操作,因为
a
b
/

我的问题:

是否可能或需要斜杠才能用于
*

贪心星(
*
)和尾星(
***
)是路径段运算符,它们将匹配路径段的其余部分,它们与段的部分不匹配,因此需要斜杠

您可以对自定义管线元素使用正则表达式来实现所需的功能:

$builder
->连接('/{key}',['controller'=>'Api','action'=>'aaaction']))
->setPatterns(['key'=>'a[a-z0-9]+']))
->setPass(['key']))
->setHost('domain.world');
$builder
->连接('/{key}',['controller'=>'Api','action'=>'bAction']))
->设置模式(['key'=>'b[a-z0-9]+']))
->setPass(['key']))
->setHost('domain.world');
$builder
->连接('/{key}',['controller'=>'Api','action'=>'cAction'])
->设置模式(['key'=>'c[a-z0-9]+']))
->setPass(['key']))
->setHost('domain.world');
另见


这正是我想要的,很有魅力,再次感谢您!