【问题标题】:Laravel email verification link issueLaravel 邮箱验证链接问题
【发布时间】:2020-08-14 15:34:54
【问题描述】:

在我的 laravel 应用程序的应用程序 url 中是这样的,admin.site,我正在从管理面板向我的应用程序注册用户。

我的客户门户网址是customer.site

一旦管理员从管理面板 (admin.site) 中创建用户,客户就会收到一封帐户验证电子邮件。但问题是现在我需要这个验证链接是

customer.site/email/...

但是当前链接是这样的

admin.site/email/...

那我怎样才能把这个验证链接改成customer.site

以下是我的客户控制器的商店功能

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.  ');
    }

我正在发送我的验证电子邮件

event(new Registered($user));

由于客户无权访问管理站点,它给了我 403 错误消息。

【问题讨论】:

  • 你能提供你使用的代码吗?
  • @Collin我已经更新了问题,这里是 event(new Registered($user));这将在创建用户时向用户发送验证邮件

标签: php laravel email laravel-5 email-verification


【解决方案1】:

对于可能发送大量电子邮件的应用程序,使用电子邮件通知很有用。 可以通过执行以下命令来创建通知:

php artisan make:notification SendRegisterEmailNotifcation

此命令将创建一个 SendRegisterEmailNotifcation 文件,可以通过导航到 app/Notifications/SendRegisterEmailNotifcation.php 路径找到该文件。 当您完成此操作并自定义消息、操作和其他可能的事情时,您的商店功能将如下所示。 我已删除验证并将其放入请求中。如果您对它的工作原理感兴趣,可以在下面找到一个示例。

更多关于通知的信息可以在这里找到: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.  ');
}

我建议创建验证数据的请求。这样控制器将保持清洁,您实际上可以验证 laravel 想要的数据。 //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',
        ];
    }
}

将通知添加到您的客户模型。必须这样做才能发送通知。

// Customer model
class Customer {
    use Notifiable;
}

【讨论】:

  • @colin 现在收到此错误,找不到错误类“App\Http\Controllers\SendRegisterEmailNotification”
  • 是的,我做了,我打电话给 $user->notify(new SendRegisterEmailNotification());在我的 stroe 方法中,而不是 event(new Registered($user));
  • @VolkaDimitrev 您是否使用了 use 语句来包含该类?
  • 是的,我添加了使用 App\Http\Notifications\SendRegisterEmailNotification;在我的控制器上
  • @Collin 我认为我的导入路径不正确,你能告诉我在控制器中使用它的正确路径是什么,这是类的路径,App\Notifications\SendRegisterEmailNotification跨度>
猜你喜欢
  • 2018-04-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-14
相关资源
最近更新 更多