Я пытаюсь использовать Laravel 5, встроенный в User Authentication. В этом отношении я хотел бы перенаправить пользователя на определенный маршрут/страницу/контроллер после успешного входа в систему. Я пытаюсь изменить код файла 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';
}
}
благодаря
Вы не должны ничего менять в 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 = '/';
поэтому после успешного входа в систему пользователь wil будет перенаправлен на redirectTo
и после выхода из системы пользователь будет перенаправлен на redirectAfterLogout
.