用户注册后的Laravel操作 [英] Laravel action after registration of user

查看:65
本文介绍了用户注册后的Laravel操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Laravel应用程序中,完成新注册后,它会自动连接到该新帐户.

In my Laravel application, after a new registration, it connects automatically to this new account.

我只需要注册并与实际的Auth帐户保持联系.我们如何更改此默认设置?

I just need to register and stay connected with the actual Auth Account. How can we change this default setting?

因为我正在使用管理员用户在应用程序中创建新帐户. 谢谢

Because I'm creating new accounts in the application with the admin user. Thank you

这是我的registerController代码:

This is my registerController code:

 use RegistersUsers;
protected function redirectTo()
{
if(Auth::user()->is_admin == 1){
  return 'persons';
}
return '/persons';
}
public function __construct()
{
    $this->middleware('auth');
}
protected function validator(array $data)
{

    return Validator::make($data, [
        'name' => 'required|string|max:255',
        'email' => 'required|string|email|max:255|unique:users',
        'password' => 'required|string|min:6|confirmed',

    ]);
}
protected function create(array $data)
{
    return User::create([
        'name' => $data['name'],
        'email' => $data['email'],
        'password' => Hash::make($data['password']),
    ]);
}

在Registeruser.php中,我将功能寄存器更改为

In Registeruser.php I changed the function register to

public function register(Request $request)
{
    $this->validator($request->all())->validate();

    event(new Registered($user = $this->create($request->all())));


    return $this->registered($request, $user)
                    ?: redirect($this->redirectPath());
}

请注意,我使用person.blade.php而不是/register

Note please that I create new users using person.blade.php, and not /register

推荐答案

在您的App/Http/Controllers/Auth/RegisterController中,您需要从RegistersUsers特征中覆盖方法register:

In your App/Http/Controllers/Auth/RegisterController you need to override the method register from RegistersUsers trait:

public function register(Request $request)
{
    $this->validator($request->all())->validate();

    event(new Registered($user = $this->create($request->all())));

    $this->guard()->login($user);

    return $this->registered($request, $user)
                    ?: redirect($this->redirectPath());
}

此行:$this->guard()->login($user);是用户登录的位置.您可以删除它或对其进行修改以适合您的需求.

This line: $this->guard()->login($user); is where the user gets logged in. You can either remove it or modify it to suit your needs.

现在,如果您要在注册后根据用户类型重定向到某个特定位置,则需要将protected $redirectTo替换为:

Now if you want to redirect after registration to a certain place depending on type of user you'd need to replace protected $redirectTo to:

protected function redirectTo()
{
    //You would need to modify this according to your needs, this is just an example.
    if(Auth::user()->hasRole('admin')){
      return 'path';
    }

    if(Auth::user()->hasRole('regular_user')){
      return 'path';
    }

    return 'default_path';
}

在文件顶部,添加以下内容:

On top of your file, add these:

use Illuminate\Http\Request; use Illuminate\Auth\Events\Registered;

use Illuminate\Http\Request; use Illuminate\Auth\Events\Registered;

这篇关于用户注册后的Laravel操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆