[英]Laravel 5.3 Redirect User after Registration
我正在使用Laravel 5.3和php artisan命令make:auth
來支持注冊和登錄。 用戶注冊后,我想將他重定向到一個頁面,該頁面顯示諸如“驗證您的電子郵件”之類的信息,並且我不希望它像默認值一樣自動登錄用戶。
我只能想到,在RegisterController的create()
方法中,我想重定向到另一個視圖,而不是返回User(我假設這是它自動登錄的地方)。
protected function create(array $data)
{
$confirmation_code = str_random(30);
Mail::to($data['email'])->send(new Company($confirmation_code));
User::create([
'confirmation_code' => $confirmation_code,
'password' => bcrypt($data['password']),
'email' => $data['email']
]);
return redirect()->route('verifyemail');
}
但是我遇到了這個錯誤: Argument 1 passed to Illuminate\\Auth\\SessionGuard::login() must implement interface Illuminate\\Contracts\\Auth\\Authenticatable, instance of Illuminate\\Http\\RedirectResponse given, called in C:\\xampp\\htdocs\\app\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Auth\\RegistersUsers.php on line 32 and defined
我試圖覆蓋register(Request $request)
方法,以刪除進行登錄的行,但仍無法正常工作。
有任何想法嗎?
編輯:我添加了$this->guard()->logout();
登錄后,覆蓋重寫的register
方法。 它可以工作,但這不是正確的方法,我想找到另一個解決方案。
我將創建自己的寄存器控制器,以提高靈活性。 然后在他們的create / store方法中創建並重定向到您想要的位置。 不會花太長時間
在app / Http / Controllers / Auth / RegisterController中,在創建方法后添加以下內容:
在這里輸入代碼
public function redirectPatch()
{
return "verifyemail";
}
為例
protected function create(array $data)
{
$confirmation_code = str_random(30);
Mail::to($data['email'])->send(new Company($confirmation_code));
User::create([
'confirmation_code' => $confirmation_code,
'password' => bcrypt($data['password']),
'email' => $data['email']
]);
}
public function redirectPatch()
{
return "/verifyemail";
}
數周以來,我一直在努力尋找解決方案。 要在注冊后覆蓋默認URL,只需在create函數內添加以下內容:
$this->redirectTo = '/url-after-register';
像這樣
protected function create(array $data) {
$this->redirectTo = '/url-after-register';
return User::create([...]);
}
謝謝! 我已經實施了您的解決方案。 或者,您可以覆蓋類中的注冊方法,然后重定向到您的特定路由。
protected function registered(Request $request, $user)
{
$this->guard()->logout();
return Redirect::route('your.route');
}
在RegisterController.php中使用這些方法
public function register(Request $request)
{
$this->validator($request->all())->validate();
event(new Registered($user = $this->create($request->all())));
Mail::to($user->email)->send(new ConfirmationEmail($user));
return back()->with('status', 'Thanks for signing up! Please check your email.');
}
public function confirmEmail($confirmation_code)
{
User::whereConfirmationCode($confirmation_code)->firstOrFail()->hasVerified();
return redirect('login')->with('status', 'You have successfully verified your account. Please Login.');
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.