How to Get Route Parameters in your route middleware?

Today now in this post i will show you How to Get Route Parameters in your route middleware? Sometimes you may need to get the route parameters value in our middleware as like if we want to check permission etc. We can get this easily by using request object, on that provide route method and also we can get it. Here I will also added small example on that way you can understand very well.

So now in the bellow route i have a id and user_id on two route and if i want to get the value of that parameters in my “check-route-param” middleware.

So at first i have route as like bellow:

Example Route:
Route::group(['middleware' => ['web','check-route-param']], function () {
    Route::get('{id}/myroute/{userid}', function () {
   	 return view('welcome');

So, now i have to “check-route-param” middleware and i can also get id and user_id value on this way:

Example Middleware:
namespace App\Http\Middleware;

use Closure;

class CheckRouteParamMiddleware
     * Handle an incoming request.
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
    public function handle($request, Closure $next)
        $id = $request->route('id');
        $userid = $request->route('userid');
        return $next($request);

Read Also: Generate Captcha code and Validation by using BotDetect package in laravel

Thanks for read. I hope it help you. For more you can follow us on facebook


About Shahriar Sagor

My name is Shahriar sagor. I'm a developer. I live in Bangladesh and I love to write tutorials and tips that will help to other Developer's. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by Shahriar Sagor →