我试图在laravel 5.1中实现内置的用户身份验证/注册,但它一直重定向到/home。
路线如下:
Route::post('sign_up' , 'Auth'AuthController@postRegister');
表单
<form method="POST" action="http://localhost:8000/sign_up" accept-charset="UTF-8" class="form-horizontal">
postRegister函数被注释掉了,所以它不应该去任何地方:
public function postRegister(Request $request)
{
// $validator = $this->validator($request->all());
//
// if ($validator->fails()) {
// $this->throwValidationException(
// $request, $validator
// );
// }
//
// Auth::login($this->create($request->all()));
//
// return redirect($this->redirectPath());
}
}
请注意,真正的逻辑发生在位于Illuminate'Foundation'Auth
命名空间中的AuthenticatesUsers
特性中。
如果你打开它,你会注意到负责检查loginPath
属性是否存在的方法loginPath()
,它负责重定向回登录路由/视图。
要更改重定向过程,请将此属性添加到AuthController.php
:
protected $loginPath = "/"; // redirect to where you want
或者,也许你想在角色或其他条件下在postRegister()
中重定向,你可以像下面的代码一样做,这是肮脏的道路,或者说不利用Laravel 5.1的新授权功能:
/**
* Handle a registration request for the application.
*
* @param 'Illuminate'Http'Request $request
* @return 'Illuminate'Http'Response
*/
public function postRegister(Request $request)
{
$validator = $this->validator($request->all());
if ($validator->fails()) {
$this->throwValidationException(
$request, $validator
);
}
// Change your loginPath here if you want: $this->loginPath = "/";
Auth::login($this->create($request->all()));
return redirect($this->redirectPath());
}
值得一提的是,另一个并行属性是$redirectTo
,它用于在成功登录后重定向到,您可以将其添加到authController.php
中,即:同样,您应该使用策略将管理员重定向到仪表板,可能将普通用户重定向到配置文件或主页!
protected $redirectTo = "dashboard";
小调整:在您的情况下为
你可能想把链接改成动态的,所以请把标题做成这样:
<form method="POST" action="{{ action('Auth'AuthController@postRegister') }}" accept-charset="UTF-8" class="form-horizontal">