Laravel case insensitive routes

后端 未结 3 1163
遥遥无期
遥遥无期 2021-01-12 16:28

How do I define a case insensitive (part of a) route?

Example:

  • Route::get(\'/{userId}/profile\');
  • http://domain.com/123/profi
相关标签:
3条回答
  • 2021-01-12 16:34

    For those using Apache you could also do this:

    At this the top of your vhost file add

    RewriteEngine On
    RewriteMap lowercase int:tolower 
    

    and in your .htaccess

    RewriteCond $1 [A-Z]
    RewriteRule ^(.*)$ /${lowercase:$1} [R=301,L]
    
    0 讨论(0)
  • 2021-01-12 16:35

    This can be solved by defining routes the following way:

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

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

    Route::pattern('profile', '(?i)profile(?-i)');
    Route::get('/{userId}/{profile}');
    
    0 讨论(0)
  • 2021-01-12 16:44

    Adding patterns only works on one route at a time, if you want all routes to be case insensitive add this to your /app/filter.php file in the before section:

    I wrote a gist which does this: https://gist.github.com/samthomson/f670f9735d200773e543

    Edit your app/filters.php to check for uppercase characters in the route and redirect them to a converted route.

    0 讨论(0)
提交回复
热议问题