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.
<?php
use App\Http\Controllers\PostController;
use Illuminate\Support\Facades\Route;
// ...
Route::get('/post/{slug}', [PostController::class, 'show'])->where('slug', '.*');
<?php
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
Thank you for this, you saved my life.
Leave a Comment
Cancel reply