Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel case insensitive routes

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

Example:

  • 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

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.

like image 243
preyz Avatar asked Feb 12 '14 14:02

preyz


People also ask

Are the routes case sensitive?

on Jul 27, 2020. The standard is that the path in a URL is case-sensitive. If you want to make case-insensitive paths work, I might look into using custom routes and redirect everything to a lower-case path.

Is laravel case sensitive?

Laravel Routing Case-insensitive routeswill match a GET request to /login but will not match a GET request to /Login . In order to make your routes case-insensitive, you need to create a new validator class that will match requested URLs against defined routes.


1 Answers

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}');
like image 97
preyz Avatar answered Oct 19 '22 07:10

preyz