2015-07-11 155 views
1

我試圖使用內置用戶身份驗證的Laravel 5.在這方面,我想重新定向用戶成功登錄後,某個路由/頁面/控制器。我試圖改變complied.php文件的代碼。我正在嘗試更改以下代碼的/home,但它不起作用。Laravel 5內置用戶身份驗證

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

感謝

回答

0

你不應該改變compiled.php

東西在RedirectIfAuthenticated中間件變化,

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

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

這基本上將登錄用戶重定向到所需的路徑,一旦登錄用戶返回到網站。 所以,handle功能按照AuthController

public $redirectTo = '/'; 
public $redirectAfterLogout = '/'; 

所以成功後登錄用戶西港島線被重定向到redirectTo和註銷的用戶將被重定向到redirectAfterLogout後添加後lookes像下面,

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

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

    return $next($request); 
} 

+0

謝謝@pinkal vansia。如果預期的路線沒有找到會發生什麼?謝謝 –

+0

你在說什麼路線?一般來說,你會得到Route找不到異常。 –

+0

謝謝@pinkal vansia。我得到這個錯誤'在編譯.php行7319: 未定義的偏移:1'中的ErrorException。 –