Laravel不区分大小写的路由 [英] Laravel case insensitive routes

查看:178
本文介绍了Laravel不区分大小写的路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何定义不区分大小写的路由(一部分)?

示例:

  • Route::get('/{userId}/profile');
  • http://domain.com/123/profile works fine.

在路线的固定部分中使用大写字母均无效:

Any use of uppercase in the fixed part of the route does not work:

  • http://domain.com/123/Profile does not work
  • http://domain.com/123/proFILE does not work

我了解如何使用-> with()使用正则表达式模式来使诸如{parameter}之类的参数,但这对如上所述的路由固定部分没有帮助.

I understand how I can make parameters like {parameter} use a regex pattern using ->with(), but that does not help me with the fixed part of the route, like described above.

推荐答案

这可以通过以下方法定义路由来解决:

This can be solved by defining routes the following way:

Route::get('/{userId}/{profile}')->with('profile', '(?i)profile(?-i)');

甚至更聪明,将其定义为pattern,然后在路线"组中也可以使用它.

Even smarter, define it as pattern, then it also becomes available in Route groups.

Route::pattern('profile', '(?i)profile(?-i)');
Route::get('/{userId}/{profile}');

这篇关于Laravel不区分大小写的路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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