jmpatricio / notifications
Laravel 的通知包
dev-master
2015-07-31 16:19 UTC
Requires
- php: >=5.4.0
- illuminate/support: 4.2.*
This package is auto-updated.
Last update: 2024-09-29 03:52:30 UTC
README
Laravel 4.2.x 包
此包提供了一个抽象层来管理 Laravel 框架中的通知(例如,电子邮件通知),这些通知由触发器引发。此包是为与 Laravel 4.2.x 一起使用而开发的,但它也可以与 5.1 LTS 一起使用。
安装
-
运行
composer require jmpatricio/notifications
-
将
Jmpatricio\Notifications\NotificationsServiceProvider
添加到config/app.php
中的服务提供者 -
安装完成。
基本用法
创建触发器
$notificationsManager = new \Jmpatricio\Notifications\Manager();
$trigger = $notificationsManager->getTriggerRepository()->add('user.newRegistration', 'When the user register');
一旦创建了触发器实例,我们就可以创建一个要触发的动作。
动作有一个配置,这取决于通知提供者。
$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);
现在我们有了所需的配置。让我们为这个触发器创建动作
$notificationsManager->getActionRepository()->add($trigger, 'email', $configuration);
此时,我们已经配置了动作,以便每次触发器 user.newRegistration
被触发时运行。
这是一个触发器的示例
// 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>";
}