Allow Slash Character in Route Parameter in Laravel 9

Allow Slash Character in Route Parameter in Laravel 9

There might be a case when URL should be defined with parameter that can contain a slash / character. It has special meaning because this character is used to separate the different URL parts.

This tutorial explains how to allow slash character in route parameter in Laravel 9 application.

By default, Laravel framework allows including all characters in route parameters except a slash / character. If this character should to be a part of route parameter, set a regular expression requirement .+ on the route using where method.



use App\Http\Controllers\PostController;
use Illuminate\Support\Facades\Route;

// ...

Route::get('/post/{slug}', [PostController::class, 'show'])->where('slug', '.*');



namespace App\Http\Controllers;

use Illuminate\Http\Response;

class PostController extends Controller
    public function show(string $slug): Response
        return new Response($slug);

Clear route cache using command:

php artisan route:clear

The 1 Comment Found

Leave a Comment

Cancel reply

Your email address will not be published.