Thanks Jarek for the point of direction. Why do you consider the middleware as a hack in this scenario? Is this the order in which laravel processes the requests?
Routes
Middleware
Controller
Or are there more layers? And if so, please correct me. Also if this isn't correct. My gut feeling says that middleware is fired before the routes thingy, but that feeling isn't based on anything.
@jacksierkstra middleware is part of the HTTP layer and is fired before hitting any route, but it is called after the routes were registered. That's why it would be hacking.
So I made a file RouteServiceProvider.php in the folder app\Providers, with the following content:
<?php
namespace App\Providers;
use App\Parse\Api\RequestParser;
use App\Parse\Api\Version;
use Illuminate\Support\ServiceProvider;
class RouteServiceProvider extends ServiceProvider
{
public function register() {}
public function map(Router $router)
{
$apiRequestParser = new RequestParser($this->app['request']);
switch ($apiRequestParser->getApiVersion()) {
case Version::Version1:
$namespace = 'App\Http\Controllers\Api\v1';
break;
case Version::Version2:
$namespace = 'App\Http\Controllers\Api\v2';
break;
default:
$namespace = 'App\Http\Controllers\Api\v1';
}
$router->group(['namespace' => $namespace], function ($router) {
require app_path('Http/routes.php');
});
}
}
At first it complained I needed a register method, so I added that in. But now, I'm wondering what should I do to register the map method?