New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
"Class insert_class_name_here
not found" - Passing parameter to Grouped Middleware
#24668
Comments
insert_class_name_here
not found" - Grouped Middlewareinsert_class_name_here
not found" - Passing parameter to Grouped Middleware
#I have this same exact issue. It turns out that you can't attach middleware parameters to a middleware group (Legit Laravel - as per the documentation and the source code). Also, this error stems from Illuminate\Foundation\Http\Kernel.php line 215 (inside the if this is to be modified to work, then the code that will have to be affected is Illuminate/Routing/Router.php line 676 (inside the public function gatherRouteMiddleware(Route $route)
{
$middleware = collect($route->gatherMiddleware())->map(function ($name) {
/*added code */
if(is_string($name) && strpos($name, ':') !== FALSE){
list($main_name, $args) = explode(':', $name);
if(array_key_exists($main_name, $this->middlewareGroups)){
$group = $this->middlewareGroups[$main_name];
// distribute the middleware param arguments to all individual middlewares that make up the group
foreach($group as $index => $middleware){
$group[$index] = "{$middleware}:{$args}";
}
$this->middlewareGroups[$main_name] = $group;
$name = $main_name;
}
}
/* end of added code */
return (array) MiddlewareNameResolver::resolve($name, $this->middleware, $this->middlewareGroups);
})->flatten();
return $this->sortMiddleware($middleware);
} As a workaround, see the below: In app/Http/Kernel.php file in your Laravel project (Which is the wrokaround i use) protected $middlewareGroups = [
.
.
'mygroup' => [
'log_event:fetch',
'mock_event:fetch'
]
];
protected $routeMiddleware = [
'log_event' =>Namespace\App\Http\Middleware\LogEvent::class,
'mock_event' =>Namespace\App\Http\Middleware\MockEvent::class
]; |
I think it looks good @synergixe , it seems to pass multiple parameters as well 😄 |
Closing as Taylor has closed the PR |
Description:
Passing parameters to a Middleware group throws a ReflectionException.
Steps To Reproduce:
Register middleware in ServiceProvider:
Pass middleware to route file registered in ServiceProvider:
This throws:
ReflectionException (-1): Class log_event does not exist
Alternative:
Passing the full class name to the middleware. This is pretty pointless because only one of my middleware classes is going through the route 😢
At this point I can just register without grouping but I was wonder if there is a way to pass a parameter to all middleware classes who belong to a group.
The text was updated successfully, but these errors were encountered: