Skip to content

Instantly share code, notes, and snippets.

@samthomson
Created August 16, 2015 09:16
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samthomson/f670f9735d200773e543 to your computer and use it in GitHub Desktop.
Save samthomson/f670f9735d200773e543 to your computer and use it in GitHub Desktop.
Case insensitive routing in laravel
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);
}
});
@paramsinghvc
Copy link

Tnx a lot!

@alexandreetf
Copy link

How can i use ?

@samthomson
Copy link
Author

@alexandreetf open the app/filters.php file, you will find the App::before function there, then just insert the conde above in.

@coreywelch
Copy link

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