Laravel 5内置用户身份验证

时间:2015-07-11 08:18:13

标签: php authentication laravel-5

我正在尝试使用内置用户身份验证的Laravel 5.在这方面,我想在成功登录后将用户重定向到某个路由/页面/控制器。我正在尝试更改代码complied.php文件。我正在尝试更改以下代码的/home,但它无效。

trait AuthenticatesAndRegistersUsers
    {
        protected $auth;
        protected $registrar;
        public function getRegister()
        {
            return view('auth.register');
        }
        public function postRegister(Request $request)
        {
            $validator = $this->registrar->validator($request->all());
            if ($validator->fails()) {
                $this->throwValidationException($request, $validator);
            }
            $this->auth->login($this->registrar->create($request->all()));
            return redirect($this->redirectPath());
        }
        public function getLogin()
        {
            return view('auth.login');
        }
        public function postLogin(Request $request)
        {
            $this->validate($request, array('email' => 'required|email', 'password' => 'required'));
            $credentials = $request->only('email', 'password');
            if ($this->auth->attempt($credentials, $request->has('remember'))) {
                return redirect()->intended($this->redirectPath());
            }
            return redirect($this->loginPath())->withInput($request->only('email', 'remember'))->withErrors(array('email' => $this->getFailedLoginMessage()));
        }
        protected function getFailedLoginMessage()
        {
            return 'These credentials do not match our records.';
        }
        public function getLogout()
        {
            $this->auth->logout();
            return redirect('/home');
        }
        public function redirectPath()
        {
            if (property_exists($this, 'redirectPath')) 
            {
                return $this->redirectPath;
            }
            return property_exists($this, 'redirectTo') ? $this->redirectTo : '/home';
        }
        public function loginPath()
        {
            return property_exists($this, 'loginPath') ? $this->loginPath : '/auth/login';
        }
    }

由于

1 个答案:

答案 0 :(得分:0)

您不应该更改compiled.php

中的任何内容

RedirectIfAuthenticated中间件更改中,

return new RedirectResponse(url('/home'));

return new RedirectResponse(url('/'));

这基本上将登录用户重定向到所需路径,一旦登录用户返回到网站。 所以,handle函数如下所示,

public function handle($request, Closure $next) {

    if ($this->auth->check())
    {
        return new RedirectResponse(url('/'));
    }

    return $next($request);
}

之后在AuthController

中添加以下内容
public $redirectTo = '/';
public $redirectAfterLogout = '/';

因此,成功登录后,用户将被重定向到redirectTo,注销后用户将被重定向到redirectAfterLogout