【问题标题】:Laravel $redirectPath doesn't workLaravel $redirectPath 不起作用
【发布时间】:2015-08-18 08:59:00
【问题描述】:

我正在使用 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']),
        ]);
    }
}

【问题讨论】:

    标签: php laravel


    【解决方案1】:

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

    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);
        }
    

    【讨论】:

      猜你喜欢
      • 2015-05-12
      • 2016-08-09
      • 2018-02-07
      • 1970-01-01
      • 1970-01-01
      • 2018-07-23
      • 2018-10-18
      • 2017-06-18
      • 2018-01-21
      相关资源
      最近更新 更多