Routing and Middleware


Basic Routing

You will define all of the routes for your application in the app/Http/routes.php file. The most basic Lumen routes simply accept a URI and a Closure:

Available Router Methods

The router allows you to register routes that respond to any HTTP verb:

Route Parameters

Of course, sometimes you will need to capture segments of the URI within your route. For example, you may need to capture a user’s ID from the URL. You may do so by defining route parameters:

You may define as many route parameters as required by your route:

Defining Middleware

To create a new middleware, copy the ExampleMiddleware that is included with the default Lumen application. In our new middleware (OldMiddleware.php), we will only allow access to the route if the supplied age is smaller than 17. Otherwise, we will redirect the users back to the “home” URI.

Registering Middleware

Global Middleware

If you want a middleware to be run during every HTTP request to your application, simply list the middleware class in the call to the $app->middleware() method in your bootstrap/app.php file:

Assigning Middleware To Routes

If you would like to assign middleware to specific routes, you should first assign the middleware a short-hand key in bootstrap/app.php file’s call to the $app->routeMiddleware() method:

Once the middleware has been defined in the HTTP kernel, you may use the middleware key in the route options array:

Use an array to assign multiple middleware to the route:

Reference :


Please enter your name here
Please enter your comment!