简体   繁体   English

按照实现Laravel本机邮件验证的步骤进行操作会引起问题

[英]Following the steps to implement Laravel native mail verification bring to an issue

Following the guide to implement the native mail verification of laravel. 按照指南实施laravel的本机邮件验证。 Brings me an error. 给我带来一个错误。

Note please that i use MongoDB, therefore i'm using Jensseger/laravel-mongodb package 请注意,我使用MongoDB,因此我使用的是Jensseger / laravel-mongodb软件包

This is the error: Class App\\User contains 3 abstract methods and must therefore be declared abstract or implement the remaining methods (Illuminate\\Contracts\\Auth\\MustVerifyEmail::hasVerifiedEmail, Illuminate\\Contracts\\Auth\\MustVerifyEmail::markEmailAsVerified, Illuminate\\Contracts\\Auth\\MustVerifyEmail::sendEmailVerificationNotification 这是错误: Class App\\User contains 3 abstract methods and must therefore be declared abstract or implement the remaining methods (Illuminate\\Contracts\\Auth\\MustVerifyEmail::hasVerifiedEmail, Illuminate\\Contracts\\Auth\\MustVerifyEmail::markEmailAsVerified, Illuminate\\Contracts\\Auth\\MustVerifyEmail::sendEmailVerificationNotification

I've already try to implement the methods inside my model and they seem to solve the problem. 我已经尝试在模型中实现方法,它们似乎可以解决问题。 But it won't send any emails. 但它不会发送任何电子邮件。

Here's what i've implemented im my User.php model 这是我在User.php模型中实现的

    * Determine if the user has verified their email address.
    *
    * @return bool
    */
    public function hasVerifiedEmail()
    {}

    /**
    * Mark the given user's email as verified.
    *
    * @return bool
    */
    public function markEmailAsVerified()
    {}

    /**
    * Send the email verification notification.
    *
    * @return void
    */
    public function sendEmailVerificationNotification()
    {}

Here's my User.php model 这是我的User.php模型

namespace App;

use App\Company;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Jenssegers\Mongodb\Auth\User as Authenticatable;

class User extends Authenticatable implements MustVerifyEmail
{
    use Notifiable;

    protected $connection = 'mongodb';

Here's my web.php route file. 这是我的web.php路由文件。

Route::get('/', function () {
    return view('welcome');
});

Auth::routes(['verify' => true]);

Route::get('/home', 'HomeController@index')->name('home');

And here's my HomeController.php 这是我的HomeController.php

    public function __construct()
    {
        $this->middleware(['auth','verified']);
    }

Here's my env file 这是我的env文件

MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=25
MAIL_USERNAME=xxxxxxxxxxx
MAIL_PASSWORD=xxxxxxxxxxxx
MAIL_ENCRYPTION=tls

Like this the project work but it wont send emails. 像这样的项目工作,但它不会发送电子邮件。 Do i need to put the logic inside the three method inside the User.php? 我是否需要将逻辑放在User.php的三个方法中? If yes what should i put in it? 如果是,我应该放什么? I've no idea because if it's native and work like charm with SQL i don't really know how to get it work on my project Hope someone has a solution for this. 我不知道,因为如果它是本机的并且可以像SQL一样吸引人,我真的不知道如何在我的项目中使用它。希望有人对此有解决方案。 Thanks 谢谢

Easiest solution is to implement trait Illuminate\\Auth\\MustVerifyEmail which should be there, however it is not mentioned in the Laravel documentation. 最简单的解决方案是实现应该存在的特征Illuminate\\Auth\\MustVerifyEmail ,但是Laravel文档中未提及。 You can also override these methods by defining them in the model as you did. 您也可以通过在模型中定义它们来覆盖这些方法。 But hasVerifiedEmail and markEmailAsVerified methods should have some verification logic and return bool based on the API . 但是hasVerifiedEmailmarkEmailAsVerified方法应该具有一些验证逻辑并根据API返回bool

Edit : I also forgot to mention that method sendEmailVerificationNotification should contain $this->notify(new Notifications\\VerifyEmail); 编辑 :我也忘记提及方法sendEmailVerificationNotification应该包含$this->notify(new Notifications\\VerifyEmail); otherwise it won't use the Notifiable trait and thus not send any email. 否则,它将不会使用“可通知”特征,因此不会发送任何电子邮件。 For more details take a look at the method in Laravel framework repository , 有关更多详细信息,请查看Laravel框架存储库中的方法,

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

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