Encode forward slashes in resource `getPages()`

https://laravel.com/docs/12.x/routing#parameters-encoded-forward-slashes The Laravel routing component allows all characters except / to be present within route parameter values. You must explicitly allow / to be part of your placeholder using a where condition regular expression:
Route::get('/search/{search}', function (string $search) {
return $search;
})->where('search', '.*');
Route::get('/search/{search}', function (string $search) {
return $search;
})->where('search', '.*');
How to do something like this in getPages()?
public static function getPages(): array
{
return [
'index' => ListResources::route('/'),
'view' => ListResource::route('/{record}/{path:.*}'),
];
}
public static function getPages(): array
{
return [
'index' => ListResources::route('/'),
'view' => ListResource::route('/{record}/{path:.*}'),
];
}
Solution:
The solution was to add the following overwrite to ViewResource.php ```php public static function route(string $path): PageRegistration {...
Jump to solution
1 Reply
Solution
Proculair B.V.
Proculair B.V.4mo ago
The solution was to add the following overwrite to ViewResource.php
public static function route(string $path): PageRegistration
{
return new PageRegistration(
page: static::class,
route: fn (Panel $panel): Route => RouteFacade::get($path, static::class)
->where('path', '.*')
->middleware(static::getRouteMiddleware($panel))
->withoutMiddleware(static::getWithoutRouteMiddleware($panel)),
);
}
public static function route(string $path): PageRegistration
{
return new PageRegistration(
page: static::class,
route: fn (Panel $panel): Route => RouteFacade::get($path, static::class)
->where('path', '.*')
->middleware(static::getRouteMiddleware($panel))
->withoutMiddleware(static::getWithoutRouteMiddleware($panel)),
);
}

Did you find this page helpful?