简体   繁体   English

Laravel email 验证链接问题

[英]Laravel email verification link issue

In my laravel application's app url is something like this, admin.site and I'm registering users to my application from the admin panel.在我的 laravel 应用程序的应用程序 url 是这样的, admin.site我正在从管理面板向我的应用程序注册用户。

And my client portal url is customer.site .我的客户门户 url 是customer.site

Once the admin creates an user from admin panel (admin.site) customer receive an account verification email.一旦管理员从管理面板 (admin.site) 中创建用户,客户就会收到帐户验证 email。 But the issue is now I need this verification link to be但问题是现在我需要这个验证链接

customer.site/email/...

but the current link is like this但是当前的链接是这样的

admin.site/email/...

So how can I change this verification link to customer.site那么如何将此验证链接更改为customer.site

Following is my store function for customer controller以下是我的商店 function 为客户 controller

public function store(Request $request)
    {
        request()->validate([
            'name' => ['required', 'alpha','min:2', 'max:255'],
            'last_name' => ['required', 'alpha','min:2', 'max:255'],
            'email' => ['required','email', 'max:255', 'unique:users'],
            'password' => ['required', 'string', 'min:12', 'confirmed','regex:/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{12,}$/'],
            'mobile'=>['required', 'regex:/^\+[0-9]?()[0-9](\s|\S)(\d[0-9]{8})$/','numeric','min:9'],
            'username'=>['required', 'string', 'min:4', 'max:10', 'unique:users'],   
            'roles'=>['required'],
            'user_roles'=>['required'],
        ]);

        //Customer::create($request->all());

        $input = $request->all();
        $input['password'] = Hash::make($input['password']);

        $user = User::create($input);
        $user->assignRole($request->input('roles'));

        event(new Registered($user));

        return redirect()->route('customers.index')
                        ->with('success','Customer created successfully. Verification email has been sent to user email.  ');
    }

I'm sending my verification email我正在发送我的验证 email

event(new Registered($user));

As the customers have no access to the admin site it gives me 403 error message.由于客户无权访问管理站点,因此它给了我 403 错误消息。

For an application that might send a lot of emails it is usefull to use notifcations for emails.对于可能发送大量电子邮件的应用程序,使用电子邮件通知很有用。 An notifaction can be created by executing the following command:可以通过执行以下命令来创建通知:

php artisan make:notification SendRegisterEmailNotifcation

This command wil create a SendRegisterEmailNotifcation file that can be found by navigating to the app/Notifications/SendRegisterEmailNotifcation.php path.此命令将创建一个SendRegisterEmailNotifcation文件,可以通过导航到app/Notifications/SendRegisterEmailNotifcation.php路径找到该文件。 When you have done that and have customized the message, action and other possible things your store function would look like this.当您完成此操作并自定义消息、操作和其他可能的内容后,您的商店 function 将如下所示。 I've removed the validation and put it in a request.我已删除验证并将其放入请求中。 If you're intrested in how it works a example can found below.如果您对它的工作原理感兴趣,可以在下面找到一个示例。

More information on notifcations can be found here: https://www.cloudways.com/blog/laravel-notification-system-on-slack-and-email/更多关于通知的信息可以在这里找到: https://www.cloudways.com/blog/laravel-notification-system-on-slack-and-email/

// CustomerController
public function store(StoreCustomerRequest $request)
{
    // Get input from the request and hash the password
    $input = $request->all();
    $input['password'] = Hash::make($input['password']);

    // Create user and assign role
    $user = User::create($input);
    $user->assignRole($request->input('roles'));

    // Send Register Email
    $user->notify(new SendRegisterEmailNotifcation);

     return redirect()->route('customers.index')
                        ->with('success','Customer created successfully. Verification email has been sent to user email.  ');
}

I would recommend creating Requests for validating the data.我建议创建验证数据的请求。 That way the controller will stay cleaner and you actually validate the data where laravel intended it to.这样,controller 将保持清洁,您实际上可以验证 laravel 想要的数据。 //StoreCustomerRequest //StoreCustomerRequest

class StoreCustomerRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return Auth::check();
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        // @todo Add validation rules
        return [
            'name' => 'required|string|max:255',
            'last_name' => 'required|alpha|min:2|max:255'
            'email' => 'required|string|email|max:255|unique:users',
        ];
    }
}

Add the notifiable to your Customer Model.将通知添加到您的客户 Model。 This has to be done to be able to send a notificaiton.必须这样做才能发送通知。

// Customer model
class Customer {
    use Notifiable;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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