【问题标题】:Laravel 5, why is a redirect to named route in middleware causing "localhost redirected you too many times"Laravel 5,为什么重定向到中间件中的命名路由导致“本地主机重定向你太多次”
【发布时间】:2017-08-13 20:12:03
【问题描述】:

我有一个非常简单的中间件:

protected $auth;

public function __construct(Guard $auth)
{
    $this->auth = $auth;
}

/**
 * Handle an incoming request.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  \Closure  $next
 * @return mixed
 */
public function handle($request, Closure $next)
{

    //dd($this->auth->user());
    if($this->auth->user()->id  && $this->auth->user()->pastDueFees()){
        \Session::flash('message','You must pay past due deal fees before using the rest of the website');
        return redirect()->route('profile.investment-fees');
    } 

    return $next($request);
}

这会导致重定向循环。我只是通过 Kernel.php 调用中间件。

我的内核.php:

<?php namespace App\Http;

使用 Illuminate\Foundation\Http\Kernel 作为 HttpKernel;

类内核扩展 HttpKernel {

/**
 * The application's global HTTP middleware stack.
 *
 * @var array
 */
protected $middleware = [
    'Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode',
    'Illuminate\Cookie\Middleware\EncryptCookies',
    'Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse',
    'Illuminate\Session\Middleware\StartSession',
    'Illuminate\View\Middleware\ShareErrorsFromSession',
    'App\Http\Middleware\VerifyCsrfToken',
    'App\Http\Middleware\FeesOwed'
];

/**
 * The application's route middleware.
 *
 * @var array
 */
protected $routeMiddleware = [
    'auth' => 'App\Http\Middleware\Authenticate',
    'auth.signed' => 'App\Http\Middleware\AuthenticateSigned',
    'fees' => 'App\Http\Middleware\FeesOwed',
    'auth.subscribed' => 'App\Http\Middleware\AuthenticateSubscribed',
    'admin' => 'App\Http\Middleware\AuthenticateAdmin',
    'auth.basic' => 'Illuminate\Auth\Middleware\AuthenticateWithBasicAuth',
    'guest' => 'App\Http\Middleware\RedirectIfAuthenticated',
];

}

提前致谢。

【问题讨论】:

  • 你的路线是什么样的?此中间件不适用于profile.investment-fees 路由,对吧?
  • 不是我没有将它应用于路由,只是将它作为中间件添加到 Kernal.php 中
  • 你的问题。如果这适用于每条路线,那么它也将适用于profile.investment-fees。当您尝试重定向到该路由时,它会尝试再次重定向,因为 pastDueFees() 没有更改
  • 确保只将中间件添加到 $routeMiddleware 而不是 $middlewareGroups$middleware
  • 有多种方法可以做到这一点。如果当前页面是 profile.investment-fees 页面,您可以在中间件中进行额外检查以不重定向。如果这意味着在每一页上,那可能是最好的。否则,您可以将其更改为像 EddyTheDoves answer 这样的路由中间件

标签: php laravel


【解决方案1】:

您需要将该中间件应用于除profile.investment.fees 之外的所有路由。在您的内核中,将您的中间件添加到 $routeMiddleware 数组中作为

'alias' => \App\Http\Middleware\MyMiddleware::class,

然后在您的路由中定义一个包含该中间件的组,并确保 profile.investment-fees 不在其中

Route::get('pif', 'MyController@pif')->name('profile.investment-fees');

//Route group
Route::group(['middleware' => 'alias'], function(){
    //every other routes that need the middleware
});

或者,在您的中间件中,您可以简单地通过使用 if else 忽略该特定路由来避免它

public function handle(Request $request, Closure $next) {
    if ($request->is('pif')) {
         return $next($request);
    }
    ...
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多