Last active
April 28, 2023 19:19
-
-
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.
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
<?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); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you for this gist!