Created
August 16, 2015 09:16
-
-
Save samthomson/f670f9735d200773e543 to your computer and use it in GitHub Desktop.
Case insensitive routing in laravel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
App::before(function($request) | |
{ | |
// if any characters in the route path are uppercase | |
if(ctype_upper(preg_replace('/[^\da-z]/i', '', $request->path()))){ | |
// extract the path section of the url (the route) from the url and convert it to lowercase | |
$sNewRelativePath = str_replace($request->path(), strtolower($request->path()), $request->fullUrl()); | |
// now redirect the user with a 301 to the lower case route | |
return \Illuminate\Support\Facades\Redirect::to($sNewRelativePath, 301); | |
} | |
}); |
This was really helpful, however middleware is now preferred in Laravel 5. Here is a gist that I created with a middleware class that is similar to this filter.
https://gist.github.com/coreywelch/2de40f93a252da3b2eddc013a98b3d37
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@alexandreetf open the app/filters.php file, you will find the App::before function there, then just insert the conde above in.