Skip to content
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

Closed
hvlucas opened this issue Jun 22, 2018 · 4 comments

Comments

@hvlucas
Copy link

hvlucas commented Jun 22, 2018

  • Laravel Version: 5.6.25
  • PHP Version: 7.1.18

Description:

Passing parameters to a Middleware group throws a ReflectionException.

Steps To Reproduce:

Register middleware in ServiceProvider:

use Namespace\App\Http\Middleware\LogEvent;
//...
$router->middlewareGroup('log_event', [LogEvent:class, ...]);

Pass middleware to route file registered in ServiceProvider:

Route::get('/model/{model_test}/', function(App\ModelTest $model_test){
    return $model_test->getAttributes();
})->middleware('log_event:fetched');

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 😢

Route::get('/model/{model_test}/', function(App\ModelTest $model_test){
    return $model_test->getAttributes();
})->middleware('Namespace\App\Http\Middleware\LogEvent:fetched');

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.

@hvlucas hvlucas changed the title "Class insert_class_name_here not found" - Grouped Middleware "Class insert_class_name_here not found" - Passing parameter to Grouped Middleware Jun 22, 2018
@synergixe
Copy link

synergixe commented Jul 7, 2018

#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 terminateMiddleware() method). Here is what i thought up...

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 gatherRouteMiddleware() method). Using your idea above, we could have this...

    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
 ];

@synergixe
Copy link

I think i should open a PR for this because i feel it (middleware group parameters spread across each individual middleware that makes up the group) is important going into Laravel 5.7 @hvlucas @themsaid

@hvlucas
Copy link
Author

hvlucas commented Jul 9, 2018

I think it looks good @synergixe , it seems to pass multiple parameters as well 😄

@laurencei
Copy link
Contributor

Closing as Taylor has closed the PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants