简体   繁体   English

Laravel 5.3注册后重定向用户

[英]Laravel 5.3 Redirect User after Registration

I'm using Laravel 5.3 and the php artisan command make:auth to scaffold the registration and login. 我正在使用Laravel 5.3和php artisan命令make:auth来支持注册和登录。 After a user registers, I want to redirect him to a page that says something like "Verify your email" and I don't want it to automatically login the user like default. 用户注册后,我想将他重定向到一个页面,该页面显示诸如“验证您的电子邮件”之类的信息,并且我不希望它像默认值一样自动登录用户。

I can only think of, in the create() method in the RegisterController, instead of returning the User(I assume that's where it automatically logins), I want to redirect to another view. 我只能想到,在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');
}

But I get this error: 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 但是我遇到了这个错误: 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

I tried to override the register(Request $request) method in the RegistersUsers.php to take out the line that makes the logins, but it's still not working. 我试图覆盖register(Request $request)方法,以删除进行登录的行,但仍无法正常工作。
Any ideas? 有任何想法吗?

EDIT:I added $this->guard()->logout(); 编辑:我添加了$this->guard()->logout(); to the overrided register method after it does the login. 登录后,覆盖重写的register方法。 It works, but it's not the correct way to do this and I would like to find another solution.. 它可以工作,但这不是正确的方法,我想找到另一个解决方案。

I would create your own register controller for more flexibility. 我将创建自己的寄存器控制器,以提高灵活性。 And in their make the create/store method that creates and then redirects to the place you want. 然后在他们的create / store方法中创建并重定向到您想要的位置。 Wouldn't take too long 不会花太长时间

in app/Http/Controllers/Auth/RegisterController add this after Method create: 在app / Http / Controllers / Auth / RegisterController中,在创建方法后添加以下内容:

enter code here 在这里输入代码

public function redirectPatch() 
{
  return "verifyemail";
} 

Exemple 为例

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

For weeks I have been trying to find a solution for this. 数周以来,我一直在努力寻找解决方案。 To override the default URL after registering, just add inside the create function the following: 要在注册后覆盖默认URL,只需在create函数内添加以下内容:

$this->redirectTo = '/url-after-register';

Just like this 像这样

protected function create(array $data) {
    $this->redirectTo = '/url-after-register';

    return User::create([...]);
}

Thanks! 谢谢! I have implemented your solution. 我已经实施了您的解决方案。 Or you can override registered method in your class and redirect to your specific route. 或者,您可以覆盖类中的注册方法,然后重定向到您的特定路由。

protected function registered(Request $request, $user)
{
    $this->guard()->logout();
    return Redirect::route('your.route');
}

Use these methods in your RegisterController.php 在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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM