utyemma / laranotice
v1.0.1
2024-04-12 20:11 UTC
Requires
- php: >=7.0
- mustache/mustache: ^2.14
README
介绍LaraNotice,您轻松创建动态通知和电子邮件消息的首选包。LaraNotice 允许您轻松地创建和发送通知,同时利用 Laravel 强大的内置通知系统。
设置和安装
在您的项目中安装LaraNotice
composer require utyemma/laranotice
如果您打算将邮件消息存储在数据库中,那么您将需要运行迁移。(注意,这是默认设置)
php artisan migrate
用法
使用Mailable类发送通知
生成mailable类
php artisan make:mailable ExampleMailable
格式化邮件消息
use App\Mailable\ExampleMailable; //Send your first notification message (new ExampleMailable)->send($user, ['mail', 'database']);
使用Laravel的邮件消息格式发送通知
您可以在Laravel 文档中了解更多关于Laravel默认邮件消息格式化的信息。
use Utyemma\LaraNotice\Notify; use App\Models\User; $users = User::all(); //Send your first notification message Notify::subject('Your Notification Subject') ->greeting('Hello!') ->line('You have a new message.') ->line('Thank you for using our application!'); ->send($users, ['mail', 'database']);
或者,如果您希望发送邮件而不是创建通知,可以通过将发送方法替换为'mail'方法来实现。
use Utyemma\LaraNotice\Notify; use App\Models\User; $recievers = ['admin@example.com', 'user@example.com']; $data = [ 'name' => 'John Doe' ]; //Send your first notification message (new Notify)->subject('Your Notification Subject', $data) ->greeting('Hello {{name}}') ->line('You have a new message.') ->line('Thank you for using our application!'); ->mail($recievers);
自定义模板引擎
默认情况下,此包使用mustache作为默认模板系统来处理基本模板数据。您可以通过php 文档了解如何使用mustache。
但是,您可以自由使用任何PHP支持的模板引擎来为您的整个应用程序或单个mailable类。您可以通过以下方式注册自定义模板解析器来实现。
为特定mailable类注册自定义模板解析器
此示例将默认解析器设置为使用Laravel的blade模板引擎。您可以在这里了解更多关于Laravel Blade的信息。
namespace App\Mailable; use Illuminate\Support\Facades\Blade; use Utyemma\LaraNotice\Notification; class ExampleMailable extends Notification { public function setResolver($content, $placeholders){ return new Blade::render($content, $placeholders); } }
为所有mailable类配置
以下是一个使用handlebars模板引擎的示例。了解如何在您的Laravel应用程序中使用Handle Bars,请参阅Handle Bars 文档。
- 创建一个简单的可调用的类,它返回一个静态的renderEngine方法
namespace App\Resolvers; use Handlebars\Handlebars; class HandleBarsResolver { function __invoke($content, $placeholders){ return (new Handlebars)->render($content, $placeholders); } }
- 更新您的配置文件中的'resolver'项
use App\Resolvers\HandleBarsResolver; return [ 'resolver' => HandleBarsResolver::class ];
当提供此类时,它将在解析模板时使用。您必须确保您的邮件消息格式基于您作为解析器使用的模板引擎。
测试
composer test
变更日志
请参阅变更日志以获取有关最近更改的更多信息。
贡献
请参阅贡献以获取详细信息。
安全漏洞
如果您发现任何与安全相关的问题,请通过utyemma@gmail.com发送电子邮件,而不是使用问题跟踪器。
鸣谢
许可证
MIT许可证(MIT)。有关更多信息,请参阅许可证文件。