Laravel $ redirectPath不起作用

时间:2015-08-17 23:35:11

标签: php laravel

我使用Laravel社交名称来启用社交登录。用户从社交网站返回后,我会重定向到/dashboard/id等表单中的信息中心页面。

现在,当我直接关闭此仪表板页面,并在浏览器中重新打开登录页面(url: auth/login)时。它开始重定向到默认/home。但有线的是,它没有通过默认的getLogin()方法,所以我在AuthController中设置的$redirectPath根本不起作用。

在sociallite中,我使用Auth::login()方法对用户进行身份验证。

Laravel版本是5.1。

任何人都可以解释这背后的逻辑是什么吗?为什么没有调用getLogin方法?

社交名流回调:

public function handleProviderCallback()
    {
        $user = Socialite::with('facebook')->user();
        $authuser = $this->findOrCreateUser($user);
        Auth::login($authuser, true);
        //todo: here it should direct to lobby.
        return redirect()->intended('/lobby/'.$authuser->id);
    }

AuthController:

class AuthController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Registration & auth Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles the registration of new users, as well as the
    | authentication of existing users. By default, this controller uses
    | a simple trait to add these behaviors. Why don't you explore it?
    |
    */

    use AuthenticatesAndRegistersUsers, ThrottlesLogins;

    protected $redirectTo = '/';
    protected $redirectPath = '/lobby';

    /**
     * Create a new authentication controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest', ['except' => 'getLogout']);
    }

    /**
     * Get a validator for an incoming registration request.
     *
     * @param  array  $data
     * @return \Illuminate\Contracts\Validation\Validator
     */
    protected function validator(array $data)
    {
        return Validator::make($data, [
            'name' => 'required|max:255',
            'email' => 'required|email|max:255|unique:users',
            'password' => 'required|confirmed|min:6',
        ]);
    }

    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return User
     */
    protected function create(array $data)
    {
        return User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']),
        ]);
    }
}

1 个答案:

答案 0 :(得分:2)

你需要在你的路线中定义像这样的“家”

Route::get('/', [   
    'uses' => 'YourController@yourMethod',
    'as' => 'home' //As home 
]);

当您将该路由设置为“home”时,您需要在RedirectIfAuthenticated.php中修改中间件

public function handle($request, Closure $next)
    {
        if ($this->auth->check()) {
            return redirect()->route('home'); //Redirect to 'home' previously defined in your routes
        }

        return $next($request);
    }