middleware not working in laravel's lumen

2.2k Views Asked by At

I have just started learning lumen micro framework and having trouble as my middleware doesn't seem to work. here's my code.

//defined middleware in route
    $app->get('/hello/{name}', ['middleware' => 'shield','uses' => 'Sampcontroller@show']);

//registered middleware in app.php
     $app->routeMiddleware([
        'shield' => App\Http\Middleware\Agemiddleware::class,
    ]);

Here it is middleware code

  public function handle($request, Closure $next){
        if ($request->input('name') == "18") {
            echo "hate yew";
        }

        return $next($request); 
   }
}
1

There are 1 best solutions below

0
Hudson Pereira On

Fix you class name (just for convention). AgeMiddleware (rename the file and the class).

Go to the bootstrap/app.php and register your route middleware

$app->routeMiddleware([
    'shield' => App\Http\Middleware\AgeMiddleware::class,
]);

Make sure you put this snippet above the return statement.

Hit /hello/18.

If this doesn't work, you probably have another route above this one getting /home/something get requests.