I'm trying to create a dynamic route prefix with middleware.
i have tried like this in my web.php
:
Route::group(['prefix' => '{role}', 'middleware'=>'operator'], function() {
Route::get('/whatever', function() {
dd('halo');
});
});
my operator middleware :
public function handle($request, Closure $next)
{
dd(Route::current()->uri());
}
but when i hit /Admin/whatever
, the output of dd is like this "{role}/whatever"
. it should be like Admin/whatever
that right ?
so the idea is, when I login let's say as Admin, I want to redirect like this /Admin/home
.
edit: also i tried this in operator middleware :
public function handle($request, Closure $next, $role)
{
dd($role));
}
but give me error Too few arguments to function...
There is data not available to middleware since it is spun up so early in the lifecycle. Session data isn't available, and it seems that the route is not fully formed before the middleware is ran. There is a different method that you can use that will return the parameters passed to the route though.
\Route::getCurrentRoute()->parameters
or $request->route()->parameters
, whichever you prefer.
This will give you a list of all parameters as {key} => value
pairs so
\Route::getCurrentRoute()->parameters['role']
or $request->route()->parameters['role']
Should give you what you're looking for.