升级到Laravel 5.2 - 索引重定向循环Auth中间件

时间:2016-01-08 10:16:51

标签: php laravel authentication routing laravel-5

我目前正在将我的一个项目从4.2升级到5.2

昨天我处于5.1中一切正常的地步,所以我开始 5.2升级。 当然,我密切关注文档中的所有步骤(https://laravel.com/docs/5.2/upgrade#upgrade-5.2.0)。

我已接近完成所有工作,但我完全陷入了由验证中间件引起的重定向循环。

以下是我如何缩小范围

经过数小时的测试和搜索后,我发现了一个非常小的设置,其中的bug仍在这里......

routes.php (其他所有评论):

<?php

/*
|--------------------------------------------------------------------------
| Routes non-ressources de l'application
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the Closure to execute when that URI is requested.
|
*/

Route::get('/', ['as'=>'index', 'uses'=>'SessionsController@create']);

SessionsController.php

<?php

class SessionsController extends \BaseController
{
    /**
     * Show the form for creating a new resource.
     *
     * @return Response
     */
    public function create()
    {
    // dd('toto');
        if (Auth::check()) {
            return Redirect::to('/accueil');
        }
        return View::make('pages.login');
    }


    /**
     * Store a newly created resource in storage.
     *
     * @return Response
     */
    public function store()
    {
        if (Auth::attempt(Input::only('email', 'password'))) {
            return Redirect::to('/accueil');
        }
        Flash::error(Lang::get('global.login_error'));
        return Redirect::back()->withInput();
    }


    /**
     * Log the user out
     *
     * @return Response
     */
    public function destroy()
    {
        Session::flush();
        Auth::logout();
        return Redirect::to('login');
    }


}

kernel.php 中间件声明:

/**
     * 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\Authenticate',
        'App\Http\Middleware\RedirectIfAuthenticated',
        // 'App\Http\Middleware\AccessMiddleware',
    ];

    /**
     * The application's route middleware.
     *
     * @var array
     */
    protected $routeMiddleware = [
        'auth' => 'App\Http\Middleware\Authenticate',
        'auth.basic' => 'Illuminate\Auth\Middleware\AuthenticateWithBasicAuth',
        // 'auth.canaccess' =>'App\Http\Middleware\AccessMiddleware',
        'guest' => 'App\Http\Middleware\RedirectIfAuthenticated',
    ];

最后,Authenticate.php中间件(基本包含在laravel 5.2中):

<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Support\Facades\Auth;
class Authenticate
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @param  string|null  $guard
     * @return mixed
     */
    public function handle($request, Closure $next, $guard = null)
    {
        if (Auth::guard($guard)->guest()) {
            if ($request->ajax()) {
                return response('Unauthorized.', 401);
            } else {
                return redirect()->guest('/');
            }
        }
        return $next($request);
    }
}

我得到的重定向循环错误

访问我的主页时,我收到此错误:Chrome中的ERR_TOO_MANY_REDIRECTS。 (在其他浏览器中出现同样的错误)

我已经知道中间件中的这条特定行会导致重定向循环:

return redirect()->guest('/');

但我无法弄清楚为什么这样做呢! - 我的索引路由不应该触发auth中间件 - 我的SessionController在它的构造函数

中不使用任何中间件

我在升级过程中遗漏了什么吗?除了 routes.php 控制器的构造函数中的声明之外,什么可能导致中间件触发?

非常感谢任何帮助=)

0 个答案:

没有答案