Laravel 5.2-更新,现在不再调用路由组中间件 [英] Laravel 5.2 - Updated and Route group Middleware now is no longer called
问题描述
因此,我使用的是最新的Laravel 5.1,今天已更新为5.2.在我的路线中,我有这样的东西:
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');
});
api版本中间件基本上检查api版本的标头,然后在route操作中适当地填充{api-namespace}.在5.1中可以正常工作.但是,自升级以来,我得到了Class App\\Http\\Controllers\\{api-namespace}\\MembersController does not exist
,它甚至都没有打到我的中间件.我感觉他们可能已经切换了代码顺序,以便在运行中间件之前验证路由操作,因为如果我将中间件全局放置,则可以正常工作.但是,我需要这个api版本组,因此,如果有人对如何解决这个问题有任何想法,我将不知所措.
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.
根据请求:
Kernel.php
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
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
更新:
我尝试将中间件移至全局,但此时它具有关于路由的0信息,这意味着它不能修改路由来替换{api-namespace}
.我认为问题在于,在中间件上运行路由之前,已经检查了路由使用",这显然是失败的.谁能确认这一点和/或向我展示我将如何在5.2中实现这样的功能?
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?
更新2:
所以我发现了这个问题.在Illuminate/Routing/Router.php:834
中,路由器尝试替换隐式绑定.这发生在中间件之前,并检查路由是否有效使用".我有0个想法如何解决此问题,因为它完全摧毁了我目前正在做的事情.有什么建议吗?
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?
推荐答案
因此,它实际上最终是一个错误,并且从laravel 5.2的最新版本开始已得到修复:请参见此处
So this actually ended up being a bug and is now fixed as of the most recent version of laravel 5.2: see here https://github.com/laravel/framework/issues/11261
这篇关于Laravel 5.2-更新,现在不再调用路由组中间件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!