我敢出任何想法現在。使用自定義中間件重定向我到/ home,即使中間件爲空Laravel
的情況是:我有一個API-端點的路徑(工作正常,響應JSON等)。
如果我現在內置的「權威性」中間件應用到路線,我總是重定向到/ home路線。看起來我正在做某事。 auth錯誤?我想錯了,這是因爲:
這種奇怪的重定向也踢,如果我不使用「權威性」,但一個自定義的中間件,沒有包含任何內容,但
public function handle($request, Closure $next)
{
print "WTF";
throw new AuthenticationException('Unauthenticated.');
}
[打印和異常從不拋出!我在/ home上再次登陸沒有錯誤]
並非所有的中間件生產這種錯誤。例如,「auth.basic」和「網絡」只是做工精細用這條路線。
我根據一些結果我發現,說,使用「網」解決了類似的問題對他們來說同樣適用「網絡」和我自己的中間件既路線,但你猜怎麼着,什麼都沒有改變。
TL:DR:如果我使用我自己的中間件或'auth',我會在中間件本身執行之前得到重定向。
Update: After fiddling around with the code and the great tipp from mnies, I found this very curious Bug:
如果我只是取消註釋的AuthenticationException,突然我的代碼如預期運行。這可能是加載Exception-Class調用RedirectIfAuthenticated Middleware ?! - 這絕對是所謂的!
現在簡單的解決方案是,對我的自定義中間件使用不同的異常,但問題是默認的'auth'-MW也使用此異常,因此導致相同的Bug。
記住:我沒有使用其他中間件不僅僅是這個自己一個人,這個bug似乎真的加載異常,像跆拳道?
所以我仍然需要幫助爲什麼會這樣!
Bug: using
throw new AuthenticationException('Unauthenticated.', []);
原因「guest'兆瓦(RedirectIfAuthenticated)被調用,而不是預期的MW-堆! (原MW-堆的什麼都沒有執行,而不管訂單。
Update 2: It seems that RedirectIfAuthenticated is thrown only because I got redirected before to the /login route (and from there as described to /home through it), but that doesn't change the issue that this random redirect occurs.
[我想ATM重現全新安裝這個Bug]
Update 3: I was not able to reproduce the bug in a fresh installation with Laravel 5.4.19.... Trying to compare both installations now. D:
使用Laravel 30年3月5日。
我的一些上下文代碼:
路線:
Route::get('/admin/user/get', ['middleware' => ['simpleauth', 'web'], 'uses' => 'UserEditAdmin\Controllers\[email protected]']);
定製中間件:
class SimpleAuth
{
public function __construct(){}
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
* @throws AuthenticationException
*/
public function handle($request, Closure $next)
{
print "WTF";
throw new AuthenticationException('Unauthenticated.');
}
}
從Kernel.php '網絡':
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,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],
'api' => [
'throttle:60,1',
'bindings',
],
];
如果你可以在Github上放一個這個問題的最簡單的例子,那將是非常好的。 – mniess