定制 utyemma/laranotice 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

utyemma/laranotice

最新稳定版本:v1.0.2

Composer 安装命令:

composer require utyemma/laranotice

包简介

README 文档

README

Latest Version on Packagist Total Downloads

Introducing LaraNotice, your go-to package for creating dynamic notifications and email messages effortlessly. LaraNotice allows you to craft and send your notifications with ease while taking advantage of Laravel's powerful built-in notification system.

Setup and Installation

Install LaraNotice in your project

composer require utyemma/laranotice

If you intend to store your mail messages in your database, then you’ll be required to run migrations. (Note that this is the default setting)

php artisan migrate

Usage

Sending Notifications using the Mailable Class

Genrate a mailable class

php artisan make:mailable ExampleMailable

Format your mail message

    use App\Mailable\ExampleMailable;

    //Send your first notification message
    (new ExampleMailable)->send($user, ['mail', 'database']);

Sending Notifications using Laravel's Mail Message Format

Your can learn more about Laravel's default mail message formatting from the Laravel Documentation

    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']);

Alternatively, if your wish to send a mail instead of create a notification, you can do so by replacing the send method with the 'mail' method

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);

Customizing the Templating Engine

By default, this package makes use of mustache as the default templating system to handle basic templating data. You can learn more about using mustache via the php documentation

However, you are free to use any custom template engine supported by PHP for your entire application or for a single mailable class. You can do so by registering custom template resolvers as shown below

Registering a custom template resolver for a specific mailable class

This example sets the default resolver to make use of Laravel's blade templating engine. You can learn more about Laravel Blade here

    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);
        }
    }
Configuring for all mailable classes

Here is an example using handlebars templating engine. Learn more about using Handle Bars in your Laravel Application. Handle Bars Docs

  1. Create a simple invokeable class that returns a static renderEngine method
    namespace App\Resolvers;

    use Handlebars\Handlebars;

    class HandleBarsResolver {

        function __invoke($content, $placeholders){
            return (new Handlebars)->render($content, $placeholders);
        }

    }
  1. Update the 'resolver' item in your config file
    use App\Resolvers\HandleBarsResolver;

    return [
        'resolver' => HandleBarsResolver::class
    ];

When this class is provided it will be used in resolving the templates. You must ensure your your mail messages are formatted based on the templating engine you are using as your resolver.

Deleting Mailables

LaraNotice provides a convenient command to clean up mailable classes and their associated database records.

# Delete a specific mailable class
php artisan mailable:delete --class=ExampleMailable

# Delete all mailable classes
php artisan mailable:delete --all

# Delete with preservation options
php artisan mailable:delete --class=ExampleMailable --preserve=database  # Only deletes the file
php artisan mailable:delete --class=ExampleMailable --preserve=files     # Only deletes from database

Command Options

  • --all: Delete all mailable classes and their database records
  • --class=: Specify a single mailable class to delete (e.g., ExampleMailable)
  • --preserve=: Optional. Specify what to keep:
    • database: Preserves database records but deletes files
    • files: Preserves files but deletes database records

The command will ask for confirmation before performing any deletions to prevent accidental data loss.

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

If you discover any security-related issues, please email utyemma@gmail.com instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

统计信息

  • 总下载量: 386
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-04-11