在本文中,我们将探索Laravel Web框架中的通知系统。 Laravel中的通知系统允许您通过不同的渠道向用户发送通知。 今天,我们将讨论如何通过邮件通道发送通知。
通知基础
在应用程序开发期间,您通常需要将不同的状态更改通知用户。 出于安全目的,可能是在更改订单状态时发送电子邮件通知,或者是发送有关其登录活动的SMS。 特别是,我们谈论的是简短的消息,只是提供对状态更改的洞察力。
Laravel已经提供了内置功能,可以帮助我们实现类似的通知。 实际上,它使向用户发送通知消息变得轻而易举,并且很有趣!
这种方法的优点在于,它允许您从不同的渠道中选择将发送通知的方式。 让我们快速浏览Laravel支持的不同通知渠道。
- 邮件:通知将以电子邮件的形式发送给用户。
- 短信:顾名思义,用户将通过手机收到短信通知。
- 松弛:在这种情况下,通知将在松弛通道上发送。
- 数据库:如果希望构建自定义UI来显示通知,则此选项允许您将通知存储在数据库中。
在不同的通知渠道中,我们将在本教程的示例用例中使用邮件渠道。
实际上,这将是一个非常简单的用例,它允许我们应用程序的用户向每个用户发送消息。 当用户在收件箱中收到新消息时,我们会通过向他们发送电子邮件来通知他们有关此事件的信息。 当然,我们将通过使用Laravel的通知功能来做到这一点!
创建一个自定义通知类
如前所述,我们将建立一个应用程序,以允许我们应用程序的用户相互发送消息。 另一方面,当用户通过电子邮件收到其他用户的新消息时,我们会通知他们。
在本节中,我们将创建实现所需的用例所需的必要文件。
首先,让我们创建一个Message
模型,该模型保存用户相互发送的消息。
$php artisan make:model Message --migration
我们还需要添加一些字段,例如to
, from
和message
到messages
表。 因此,让我们更改迁移的文件运行前migrate
命令。
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateMessagesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('messages', function (Blueprint $table) {
$table->increments('id');
$table->integer('from', FALSE, TRUE);
$table->integer('to', FALSE, TRUE);
$table->text('message');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('messages');
}
}
现在,让我们运行migrate命令在数据库中创建messages表。
$php artisan migrate
那应该在数据库中创建messages
表。
另外,请确保首先启用了默认的Laravel身份验证系统,以便开箱即用的功能(例如注册和登录)。 如果您不确定该怎么做,Laravel 文档将为您提供快速的了解。
由于Laravel中的每个通知都由一个单独的类表示,因此我们需要创建一个自定义通知类,该类将用于通知用户。 让我们使用以下artisan命令创建自定义通知类NewMessage。
$php artisan make:notification NewMessage
那应该创建app/Notifications/NewMessage.php
类,所以让我们用以下内容替换该文件的内容。
<?php
// app/Notifications/NewMessage.php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use App\User;
class NewMessage extends Notification
{
use Queueable;
public $fromUser;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct(User $user)
{
$this->fromUser = $user;
}
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return ['mail'];
}
/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
$subject = sprintf('%s: You\'ve got a new message from %s!', config('app.name'), $this->fromUser->name);
$greeting = sprintf('Hello %s!', $notifiable->name);
return (new MailMessage)
->subject($subject)
->greeting($greeting)
->salutation('Yours Faithfully')
->line('The introduction to the notification.')
->action('Notification Action', url('/'))
->line('Thank you for using our application!');
}
/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toArray($notifiable)
{
return [
//
];
}
}
当我们要使用邮件通道向用户发送通知时,将相应地配置via
方法。 因此,这是允许您配置通知的通道类型的方法。
接下来,有toMail
方法,允许您配置各种电子邮件参数。 实际上, toMail
方法应该返回\Illuminate\Notifications\Messages\MailMessage
的实例,并且该类提供了有用的方法,允许您配置电子邮件参数。
在多种方法中, line
方法允许您在消息中添加一行。 另一方面,可以使用action
方法在消息中添加号召性用语按钮。
这样,您可以格式化将发送给用户的消息。 因此,这就是在使用邮件通道发送通知时配置通知类的方式。
最后,您需要确保根据via
方法中配置的通道类型实现必要的方法。 例如,如果您使用的是将通知存储在数据库中的数据库通道,则无需配置toMail
方法。 相反,您应该实现toArray
方法,该方法格式化需要存储在数据库中的数据。
如何发送通知
在上一节中,我们创建了一个准备发送通知的通知类。 在本节中,我们将创建文件来演示如何使用NewMessage
通知类实际发送通知。
让我们在app/Http/Controllers/NotificationController.php
创建一个具有以下内容的控制器文件。
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use App\Message;
use App\User;
use App\Notifications\NewMessage;
use Illuminate\Support\Facades\Notification;
class NotificationController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function index()
{
// user 2 sends a message to user 1
$message = new Message;
$message->setAttribute('from', 2);
$message->setAttribute('to', 1);
$message->setAttribute('message', 'Demo message from user 2 to user 1.');
$message->save();
$fromUser = User::find(2);
$toUser = User::find(1);
// send notification using the "user" model, when the user receives new message
$toUser->notify(new NewMessage($fromUser));
// send notification using the "Notification" facade
Notification::send($toUser, new NewMessage($fromUser));
}
}
当然,您需要在routes/web.php
文件中添加关联的routes/web.php
。
Route::get('notify/index', 'NotificationController@index');
Laravel有两种发送通知的方式:使用可通知实体或Notification门面。
如果实体模型类利用Illuminate\Notifications\Notifiable
特征,则可以在该模型上调用notify
方法。 App\User
类实现了Notifiable
特质,因此它成为了Notifiable
实体。 另一方面,您也可以使用Illuminate\Support\Facades\Notification
Facade将通知发送给用户。
我们来看一下控制器的index
方法。
就我们而言,我们将在用户收到新消息时通知他们。 因此,我们首先尝试在index
方法中模仿该行为。
接下来,我们使用$toUser
对象上的notify
方法将$toUser
通知了接收者用户,因为它是可通知的实体。
$toUser->notify(new NewMessage($fromUser));
您可能已经注意到,我们还要在__construct
方法的第一个参数中传递$fromUser
对象,因为我们希望在消息中包含from用户名。
另一方面,如果您想使用Notification
Facade模仿它,则使用以下代码片段很容易做到。
Notification::send($toUser, new NewMessage($fromUser));
如您所见,我们已经使用了Notification门面的send
方法将通知发送给用户。
继续并在浏览器中打开URL http:// your-laravel-site-domain / notify / index 。 如果您尚未登录,您将被重定向到登录屏幕。 登录后,您应该在用户1
随附的电子邮件地址上收到一封通知电子邮件。
你可能想知道的通知系统如何检测to
地址时,我们还没有配置它的任何地方呢。 在这种情况下,通知系统将尝试在可通知对象中查找email
属性。 当我们使用默认的Laravel身份验证系统时, App\User
对象类已经具有该属性。
但是,如果您想覆盖此行为,并且想要使用电子邮件以外的其他属性,则只需在通知类中定义以下方法。
public function routeNotificationForMail()
{
return $this->email_address;
}
现在,通知系统应该寻找email_address
财产,而不是email
属性来获取to
的地址。
这就是在Laravel中使用通知系统的方法。 这也将我们带到了本文的结尾!
结论
我们今天所经历的是Laravel的有用但尚未讨论的功能之一-通知。 它允许您通过不同的渠道向用户发送通知。
快速介绍之后,我们实现了一个真实的示例,该示例演示了如何通过邮件通道发送通知。 实际上,在发送有关应用程序状态更改的短消息的情况下,它非常方便。
对于那些刚刚开始使用Laravel或希望通过扩展来扩展您的知识,网站或应用程序的人,我们可以在Envato Market中进行很多研究。
如果您有任何疑问或建议,请立即使用以下供稿发布!
翻译自: https://code.tutsplus.com/tutorials/notifications-in-laravel--cms-30499