Chris Ganga
Today we’ll be talking about how to use route parameters in middleware, a task that may come up in your workflow.
A middleware simply wraps an application’s request. Think of it this way, when a request is made to your application, there’s code that handles the request, but if you want something to occur before or after the code that handles the request is run, you would put in a middleware.
Laravel comes with a few inbuilt middlewares for maintenance, authentication and CSRF protection, which are all found in the app/Http/Middleware directory inside a laravel project folder.
Laravel routes are located in the app/Http/routes.php file. A route usually has the URL path, and a handler function callback, which is usually a function written in a certain controller.
A parameter provided in the route is usually annotated with curly braces. For instance, to pass in a name parameter to a route, it would look like this.
By convention, the Controller function accepts parameters based on the parameters provided.
Accessing parameters in a middleware however is not that direct. First let’s create a middleware called DumpMiddleware
that dumps the parameters provided in the route.
The file app/Http/Middlware/DumpMiddleware.php is created.
$request->route('parameter_name')
We can access route parameters in two ways. One way is by using $request->route('parameter_name')
., where parameter_name
refers to what we called the parameter in the route. In the handle method within the DumpMiddleware class created in the app/Http/Middleware/DumpMiddleware.php file.
To see the middleware in action, let’s add it to the route. Quickly create the route we showed above and add the middleware in the app/Http/routes.php file.
We need the full namespace of the middleware, because we have not made Laravel aware of our new middleware. To do this we add it in the App\Http\Kernel.php file, as part of the $routeMiddleware
class property.
You can then update the middleware in the route to be 'middleware' => 'DumpMiddleware',
.
Run php artisan serve
on your terminal and open http://localhost:8000/params/scotch
. You should see scotch dumped in the browser.
$request->route()->paremeters()
The other way to access the Route parameters in the middlware involves gettting all the parameters. Imagine you had a route like this /params/{id}/{category}/{name}
. All the route params are usually saved in an array, which in a middleware is accessible through $request->route()->parameters()
. If we update our route to the above route,
And update the middleware’s handle method to,
Going to http://localhost:8000/params/23/scotch/school
should dump the parametes in your browser as an array, with the parameter names as the keys, and what was passed as values.
A sample usage for accessing route parameters in middleware is when a given route performs different actions. Say for instance you have a route that returns user’s profiles, /{username}
. To go to Scotch’s profile for instance, one would go to http://localhost:8000/scotch
.
This app, however, may have some routes that do not necessarily belong to profiles. Like faq, or support, or help. In such a scenario, it is usually recommended to have these routes defined before the route that looks for a profile.
app/http/routes.php
With middleware however, all we need to do is check if a value is in a defined array and redirect.
app/Http/routes.php
Then in the profile middleware, we add this in the handle function.
We simply check if the parameter is in a given array, and return a view based on that. In case we have any other keywords that are not neccesarrily profiles, all we need to do is add them to this array. Cleaner, right?
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
To my knowledge, laravel routes are located in routes/* directory.
See here: https://github.com/laravel/laravel/tree/master/routes