2

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); 
   }
}
Pradeep Sapkota
  • 2,041
  • 1
  • 16
  • 30
vijay varikol
  • 81
  • 3
  • 10

1 Answers1

0

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.

Hudson Pereira
  • 1,066
  • 8
  • 13