coka/notifier-message
Base library providing a common message class for notifier implementations. Install via Composer and extend it to standardize notification message data across your PHP/Laravel channels. Includes changelog/upgrade notes and MIT license.
Installation
composer require coka/notifier-message
Register the service provider in config/app.php:
'providers' => [
// ...
CedrickOka\NotifierMessage\NotifierMessageServiceProvider::class,
],
Publish Config (Optional)
php artisan vendor:publish --provider="CedrickOka\NotifierMessage\NotifierMessageServiceProvider"
Config file: config/notifier-message.php.
First Use Case
Send a basic message via a channel (e.g., mail or slack):
use CedrickOka\NotifierMessage\Facades\NotifierMessage;
NotifierMessage::send('user@example.com', 'welcome', [
'name' => 'John Doe',
]);
Channel-Based Messaging
Define channels in config/notifier-message.php:
'channels' => [
'mail' => [
'driver' => 'mail',
'options' => [
'from' => 'noreply@example.com',
],
],
'slack' => [
'driver' => 'slack',
'options' => [
'webhook_url' => env('SLACK_WEBHOOK_URL'),
],
],
],
Send via a channel:
NotifierMessage::channel('slack')->send('team-channel', 'alert', ['message' => 'System down!']);
Custom Templates
Store templates in resources/views/notifier-message/. Example:
resources/
└── views/
└── notifier-message/
├── mail/welcome.blade.php
└── slack/alert.blade.php
Reference in config:
'templates' => [
'welcome' => 'mail::welcome',
'alert' => 'slack::alert',
],
Event-Driven Triggers
Listen for events (e.g., user.registered) and dispatch messages:
use CedrickOka\NotifierMessage\Events\MessageSent;
event(new MessageSent('user@example.com', 'welcome'));
Illuminate\Notifications\Notification:
NotifierMessage::extend('notification', function ($notifiable, $notification) {
return NotifierMessage::send($notifiable->getEmail(), $notification->via(), $notification->toArray());
});
NotifierMessage::later(now()->addMinutes(5))->send('user@example.com', 'reminder');
Channel Driver Mismatch
Ensure the driver in config/notifier-message.php matches a registered channel (e.g., mail, slack). Defaults to mail if unspecified.
Template Paths
Blade templates must follow the resources/views/notifier-message/{channel}/{template}.blade.php structure. Misspelled paths throw TemplateNotFoundException.
Async Failures
If using queues, unhandled exceptions in channel drivers (e.g., Slack webhook failures) won’t trigger Laravel’s failed job handler. Wrap in try-catch:
try {
NotifierMessage::send('user@example.com', 'welcome');
} catch (\Exception $e) {
\Log::error("Message failed: " . $e->getMessage());
}
Log Output: Enable debug mode in config:
'debug' => env('NOTIFIER_DEBUG', false),
Logs will appear in storage/logs/laravel.log.
Channel-Specific Errors:
Check config/notifier-message.php for options like timeout (Slack) or from (Mail). Invalid configs may silently fail.
Custom Channels Create a new channel class:
namespace App\Channels;
use CedrickOka\NotifierMessage\Contracts\Channel;
class CustomChannel implements Channel {
public function send($recipient, $template, array $data) {
// Custom logic (e.g., SMS, Telegram)
}
}
Register in config:
'channels' => [
'custom' => [
'driver' => 'custom',
'class' => App\Channels\CustomChannel::class,
],
],
Middleware Add middleware to modify messages before sending:
NotifierMessage::middleware(function ($message) {
$message->data['footer'] = '© 2023';
});
Testing Mock the facade in tests:
$this->partialMock(NotifierMessage::class, 'send')
->shouldReceive('send')
->once()
->with('user@example.com', 'welcome', ['name' => 'John']);
How can I help you explore Laravel packages today?