如果隐藏的输入字段已更改为无效值,我正在尝试将用户重定向到主页,但由于create方法必须返回用户实例,因此我无法在此处执行此操作。那么我的选择是什么?
/**
* Create a new user instance after a valid registration.
*
* @param array $data
* @return User
*/
protected function create(array $data)
{
$role = Role::where('name', $data['role'])->first();
// Hidden role field has been tampered with! Redirect to homepage.
if(!$role){
return redirect('/');
}
$user = new User();
$user->email = $data['email'];
$user->password = bcrypt($data['password']);
$user->save();
$user->roles()->attach($role);
return $user;
}
以上代码致命错误:
FatalThrowableError in SessionGuard.php line 418:
Type error: Argument 1 passed to Illuminate\Auth\SessionGuard::login() must be an instance of Illuminate\Contracts\Auth\Authenticatable, instance of Illuminate\Http\RedirectResponse given, called in E:\xampp\htdocs\taskie\vendor\laravel\framework\src\Illuminate\Foundation\Auth\RegistersUsers.php on line 63
答案 0 :(得分:0)
在使用工匠AuthController
生成的make:auth
中,注册用户请求由方法register
处理,而不是create
。此方法在特征Illuminate\Foundation\Auth\RegistersUsers
中实现。您可以在控制器中重载此方法并更改其行为。
/**
* Handle a registration request for the application.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function register(Request $request)
{
$data = $request->all();
$role = Role::where('name', $data['role'])->first();
// Hidden role field has been tampered with! Redirect to homepage.
if(!$role){
return redirect('/');
}
return parent::register($response);
}
答案 1 :(得分:0)
我通过创建自定义验证解决了这个问题。
'email' => ['required','max:255','unique:users','regex:/(@org.co.uk)$/',],
现在不需要重定向。