So I was on the latest Laravel 5.1 and updated to 5.2 today. In my routes I have something like this:
Route::group(['middleware' => ['api']], function() {
// Members
Route::get('members', '{api-namespace}\MembersController@index');
Route::get('member/{id}', '{api-namespace}\MembersController@show');
// Members Pension
Route::get('member/{id}/pension/beneficiaries', '{api-namespace}\Inquiry\MembersPensionController@showBeneficiaries');
Route::get('member/{id}/pension/contributions', '{api-namespace}\Inquiry\MembersPensionController@showContributions');
Route::get('member/{id}/pension/yearlySummary', '{api-namespace}\Inquiry\MembersPensionController@showYearlySummary');
Route::get('member/{id}/pension/pensioners', '{api-namespace}\Inquiry\MembersPensionController@showPensioners');
// Members Summary
Route::get('member/{id}/beneficiaries/{fund?}', '{api-namespace}\BeneficiariesController@showByMember');
Route::get('member/{id}/contributions/', '{api-namespace}\ContributionsController@showByMember');
// Beneficiaries
Route::get('beneficiaries', '{api-namespace}\BeneficiariesController@index');
Route::get('beneficiary/{id}', '{api-namespace}\BeneficiariesController@show');
// Contributions
Route::get('contributions', '{api-namespace}\ContributionsController@index');
Route::get('users', '{api-namespace}\UsersController@index');
});
The api-version middleware basically checks the header for an api-version and then fills in {api-namespace} in the route actions appropriately. This works fine in 5.1. However, since upgrading I get Class App\\Http\\Controllers\\{api-namespace}\\MembersController does not exist
and it doesn't even hit my middleware at all. I have a feeling that they may have switched the order of the code so that it validates the route actions BEFORE running middleware because if I put the middleware in globally it works fine. However, I need this api-version group, so if anyone has any ideas how to get around this I am all ears.
As per request:
Kernel.php
<?php
namespace App\Http;
use Illuminate\Foundation\Http\Kernel as HttpKernel;
class Kernel extends HttpKernel
{
/**
* The application's global HTTP middleware stack.
*
* @var array
*/
protected $middleware = [
\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
];
/**
* The application's route middleware.
*
* @var array
*/
protected $routeMiddleware = [
'auth' => \App\Http\Middleware\Authenticate::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'api-version' => \App\Http\Middleware\ApiVersionMiddleware::class,
];
/**
* The application's route middleware groups.
*
* @var array
*/
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
],
'api' => [
'throttle:60,1',
'api-version',
'cors'
],
];
}
ApiVersionMiddleware.php
<?php namespace App\Http\Middleware;
use Closure;
use Jbm\Exceptions\ApiVersionException;
use Jbm\Helpers\ApiVersion;
class ApiVersionMiddleware
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
$route = $request->route();
$actions = $route->getAction();
$requestedApiVersion = ApiVersion::get($request);
if (!ApiVersion::isValid($requestedApiVersion)) {
throw new ApiVersionException('Invalid API Version');
}
$apiNamespace = ApiVersion::getNamespace($requestedApiVersion);
$actions['uses'] = str_replace(
'{api-namespace}', $apiNamespace, $actions['uses']
);
$route->setAction($actions);
return $next($request);
}
}
Also notice that I moved my middleware into a middleware group and its still not working. Just an fyi
Update:
I tried moving the middleware to global but at that point it has 0 information about the route which means that it can't modify the route to replace {api-namespace}
. I believe the issue is that the route 'uses' is checked BEFORE middleware is run on it which obviously fails. Can anyone confirm this and / or show me how i would implement something like this in 5.2?
Update 2:
So I have discovered the issue. In Illuminate/Routing/Router.php:834
the router tries to substitute implicit bindings. This happens BEFORE middleware and checks for the route for valid "uses". I have 0 idea how to work around this as it totally blows away what I am currently doing. Any suggestions?