Silex路由可互换参数

问题描述 投票:0回答:1

鉴于我有一个必须接受对象的代码或id的路由,在silex中是否有任何方法可以检测哪个传递给路由(数字或非数字)然后发送一个变量或另一个变量?

例如。 / route / 1 - >数据库上的美国ID

我将$ id = 1和$ code = null发送给控制器

/ route / US - >数据库上的美国代码

我将$ id = null和$ code ='US'发送给控制器

我试过这样的东西,但它不会起作用

$apiRoutesV2
    ->get('/route/{code}{id}', 'controllers.myController:getIndex')
    ->value('id', null)
    ->assert('id', '[0-9]+')
    ->value('code', null)
    ->assert('code', '[a-zA-Z]+');
silex
1个回答
1
投票

如果id和code都可以为空,则可以使用默认值创建一个路径:

$app
    ->get('/route/{code}{id}', 'controllers.myController:getIndex')
    ->value('id', '')
    ->assert('id', '[0-9]*')
    ->value('code', '')
    ->assert('code', '[a-zA-Z]*');

如果要填充其中一个参数,请添加2条路线:

$app
    ->get('/route/{id}', 'controllers.myController:getIndex')
    ->assert('id', '[0-9]+');

$app
    ->get('/route/{code}', 'controllers.myController:getIndex')
    ->assert('code', '[a-zA-Z]+');
© www.soinside.com 2019 - 2024. All rights reserved.