Couldn't find anything that specifically matches my situation. I have a route group defined as:
Route::group(['prefix' => 'api/v1/{access_token}'], function(){
...
}
The above group has several resource routes inside. I am trying to create a custom middleware that will validate the access_token
parameter and return a 400 response if the parameter is not valid. I would like to be able to so something like this in my controllers:
class ProductController extends Controller {
/**
* Instantiate a new ProductController
*/
public function __construct()
{
$this->middleware('verifyAccessToken');
}
...
}
My question is not "how do I define custom middleware", but rather, how can I gain access to the access_token
parameter from within the handle
function of my custom middleware?
EDIT: While the question suggested as a duplicate is similar and has an answer, that answer seems to be outdated and/or unsatisfactory for what I am trying to accomplish.
You can just access it from your $request
object using the magic __get
method like this:
public function handle($request, Closure $next)
{
$token = $request->access_token;
// Do something with $token
}
http://laravel.com/docs/master/middleware#middleware-parameters
For simple
public function yourmethod($access_token){
$this->middleware('verifyAccessToken', $access_token);
}
I think you can't do it in __construct()
method.
Just stick the middleware on the Route::group
Route::group(['prefix' => 'api/v1/{access_token}', 'middleware' => 'verifyAccessToken'], function(){
});
Then in your middleware, as Thomas Kim pointed out, you can use the $request
object to gain access to the token that was passed to the route.
public function handle($request, Closure $next)
{
$token = $request->access_token;
// Do something with $token
}