I am busy building a Restful API in Laravel 5.1 where the API version is passed through the header. This way I can version the features rather than copy and pasting a whole route group and increment the version number.
The problem I'm having is that I would like to have versioned methods, IE:
public function store_v1 (){ }
I have added a middleware on my routes where I capture the version from the header, but now need to modify the request to choose the correct method from the controller.
app/Http/routes.php
Route::group(['middleware' => ['apiversion']], function()
{
Route::post('demo', 'DemoController@store');
}
app/Http/Middleware/ApiVersionMiddleware.php
public function handle($request, Closure $next)
{
$action = app()->router->getCurrentRoute()->getActionName();
// dd($action)
// returns "App\Http\Controllers\DemoController@store"
}
From here on, I would attach the header version to the $action and then pass it through the $request so that it reaches the correct method.
Well, that is the theory anyway.
Any ideas on how I would inject actions into the Route?