Laravel – How to Get Route Parameters in your route middleware?

Laravel – How to Get Route Parameters in your route middleware?

In this post we will give you information about Laravel – How to Get Route Parameters in your route middleware?. Hear we will give you detail about Laravel – How to Get Route Parameters in your route middleware?And how to use it also give you demo for it if it is necessary.

Sometimes we may require to get route parameters value in our middleware like if you want to check permission etc. You can get easily using request object, that provide route method and you can get it. I also added small example that way you can undestand very well.

In this bellow route i have id and userid two route and i want to get value of that parameters in my “check-route-param” middleware so first i have route like:

Example Route:

Route::group(['middleware' => ['web','check-route-param']], function () {

Route::get('{id}/myroute/{userid}', function () {

return view('welcome');

});

});

So, i have “check-route-param” middleware and i can get id and userid value this way:

Example Middleware:

namespace AppHttpMiddleware;


use Closure;


class CheckRouteParamMiddleware

{

/**

* Handle an incoming request.

*

* @param IlluminateHttpRequest $request

* @param Closure $next

* @return mixed

*/

public function handle($request, Closure $next)

{

$id = $request->route('id');

$userid = $request->route('userid');

return $next($request);

}

}

Hope this code and post will helped you for implement Laravel – How to Get Route Parameters in your route middleware?. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

3  +  1  =  

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US