所以我参加了最新的Laravel 5.1并且今天更新到了5.2。在我的路线中我有这样的事情:
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');
});
api-version中间件基本上检查api版本的头部,然后适当填写路由动作中的{api-namespace}。这在5.1中工作正常。但是,自升级以来,我得到了Class App\\Http\\Controllers\\{api-namespace}\\MembersController does not exist
,它甚至根本没有打到我的中间件。我有一种感觉,他们可能已经切换了代码的顺序,以便在运行中间件之前验证路由操作,因为如果我将中间件放在全局中它可以正常工作。但是,我需要这个api-version组,所以如果有人有任何想法如何解决这个问题我很满意。
根据要求:
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);
}
}
另请注意,我已将中间件移至中间件组,但仍无法正常工作。只是一个假的
更新
我尝试将中间件移动到全局,但此时它有0个关于路由的信息,这意味着它无法修改路由以替换{api-namespace}
。我认为问题在于路线使用&#39;在中间件运行之前检查它显然是失败的。任何人都可以确认这个和/或告诉我如何在5.2中实现这样的东西吗?
更新2:
所以我发现了这个问题。在Illuminate/Routing/Router.php:834
中,路由器尝试替换隐式绑定。这种情况发生在中间件之前,并检查路由是否有效&#34;使用&#34;。我不知道如何解决这个问题,因为它完全打破了我目前正在做的事情。有什么建议吗?
答案 0 :(得分:0)
所以这实际上最终成了一个错误,现在已经修复了laravel 5.2的最新版本:请参阅此处https://github.com/laravel/framework/issues/11261