Skip to content

Instantly share code, notes, and snippets.

@coreywelch
Last active April 28, 2023 19:19
Show Gist options
  • Save coreywelch/2de40f93a252da3b2eddc013a98b3d37 to your computer and use it in GitHub Desktop.
Save coreywelch/2de40f93a252da3b2eddc013a98b3d37 to your computer and use it in GitHub Desktop.
Here is a middleware class for laravel 5 that takes incoming routes and forces them to lowercase.
<?php
namespace App\Http\Middleware;
use Closure;
use \Illuminate\Support\Facades\Redirect;
class LowercaseRoutes
{
/**
* Run the request filter.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
if(!ctype_lower(preg_replace('/[^A-Za-z]/', '', $request->path()))){
$new_route = str_replace($request->path(), strtolower($request->path()), $request->fullUrl());
return Redirect::to($new_route, 301);
}
return $next($request);
}
}
@Vitsen15
Copy link

Vitsen15 commented Jan 4, 2021

Thank you for this gist!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment