samedi 1 juin 2019

Laravel middleware parameter error: undefined variable

I'm using Laravel 5.8 and I've created a middleware:

class ResourceMiddleware {
    public function handle($request, Closure $next, $resource){}
}

registered in kernel.php as

protected $routeMiddleware = [
    'resource' => 'App\Http\Middleware\ResourceMiddleware',
];

created a route like this:

Route::get(
    '/resource', 
    [
        'middleware' => 'resource:test', 
        'uses' => function() {
            return 'My resource';
        }
    ]
);

When I access GET /resource I got an error: undefined variable Somehow Laravel expects that the param name in ResourceMiddleware class should be $test. If I change the param name to $test, no error haapens and the $test value will be "test". What I'm doing wrong?

I tried different syntax like 'middleware' => ['resource:test'] or ->middleware('resource:test'). None worked.

There's a syntax that defines the param name and value? The documentation states that the syntax is middleware:value. Why the param name should be equals to value?

Best regards.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire