我的应用程序中的路由和回调之间存在以下关联:
$api->slim->post('/:accountId/Phone-Numbers/', function($accountId) use ($api) {
$api->createPhoneNumber($accountId);
});
Run Code Online (Sandbox Code Playgroud)
我想要避免的是让路由myhost/a7b81cf/phone-numbers/返回404响应,因为Slim将路由理解myhost/a7b81cf/Phone-Numbers/为不同,因为使用了大写字母.如何避免设置两个触发相同回调函数的独立路由?
这是一个老问题,但我想为这个问题提供一个明确的答案.
有一个'routes.case_sensitive'配置允许您这样做.我不知道为什么,这不是在文档(http://docs.slimframework.com/#Application-Settings),但如果你在框架的源代码看(具体而言,在getDefaultSettings()在Slim.php你可以看到那里.
我只是测试它,它工作正常.
总而言之,解决方案是应用'routes.case_sensitive'配置,如下所示:
$configurations = [
// ... other settings ...
'routes.case_sensitive' => false
];
$app->config($configurations);
Run Code Online (Sandbox Code Playgroud)
您可以尝试使用路线验证/条件:
$api->slim->post('/:accountId/:phoneNumbers/', function ($accountId) {
$api->createPhoneNumber($accountId);
})->conditions(array('phoneNumbers' => '(p|P)hone\-(n|N)umbers'));
Run Code Online (Sandbox Code Playgroud)
或者,如果您想要更多全局更改,您可以覆盖 Route 类中的 Route->matches 方法,使其不区分大小写,但这会影响整个应用程序。
| 归档时间: |
|
| 查看次数: |
1184 次 |
| 最近记录: |