jmpatricio / notifications
Notifications package for Laravel
Requires
- php: >=5.4.0
- illuminate/support: 4.2.*
This package is auto-updated.
Last update: 2024-11-29 04:08:57 UTC
README
Laravel 4.2.x Package
This package provides an abstract layer to manage notifications (eg. email notifications) in Laravel Framework, fired by triggers. This package was developed to work with Laravel 4.2.x but it will work with 5.1 LTS.
Instalation
-
Run
composer require jmpatricio/notifications
-
Add
Jmpatricio\Notifications\NotificationsServiceProvider
to your service providers inconfig/app.php
-
Instalation Done.
Basic usage
Create the trigger
$notificationsManager = new \Jmpatricio\Notifications\Manager();
$trigger = $notificationsManager->getTriggerRepository()->add('user.newRegistration', 'When the user register');
Once we have the trigger instace created, we can create an action to be fired.
An action have a configuration, which depends of the notification provider.
$configuration = [
'view' => 'emails/newUserRegistration',
'fromEmail' => 'noreply@mywebsite.dev',
'fromName' => 'Notifications',
'toEmail' => 'some@email.dev',
'toName' => 'Staff user',
'subject' => 'New user registered!'
];
$configuration = \Jmpatricio\Notifications\Providers\Configuration::createFromArray($configuration);
Now we have the desired configuration. Let's create the action for this trigger
$notificationsManager->getActionRepository()->add($trigger, 'email', $configuration);
At this point we already have the action configured to run every time the trigger user.newRegistration
is fired.
This is an example to fire the trigger:
// Payload will be the new user object This is only an example
$payload = new stdClass();
$payload->name = 'John';
try {
$notificationsManager->fire($trigger->slug, $payload);
} catch (\Jmpatricio\Notifications\Exceptions\ConfigurationNotValidException $e){
echo "<pre>";
print_r($e->getErrors());
echo "</pre>";
}