-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
166 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,30 +29,75 @@ to the require section of your `composer.json` file. | |
Usage | ||
----- | ||
|
||
Once the extension is installed, simply use it in your code by : | ||
The following example shows how to create a Notifier instance and send your first notification: | ||
|
||
Widget | ||
```php | ||
$notifier = new \tuyakhov\notifications\Notifier([ | ||
'channels' => [...], | ||
]); | ||
$notifier->send($recipients, $notifications); | ||
``` | ||
|
||
Notifier is often used as an application component and configured in the application configuration like the following: | ||
|
||
```php | ||
\tuyakhov\notifications\EmbedWidget::widget([ | ||
'code' => 'vs5ZF9fRDzA', | ||
'playerParameters' => [ | ||
'controls' => 2 | ||
], | ||
'iframeOptions' => [ | ||
'width' => '600', | ||
'height' => '450' | ||
] | ||
]); | ||
[ | ||
'components' => [ | ||
'notifier' => [ | ||
'class' => '\tuyakhov\notifications\Notifier', | ||
'channels' => [ | ||
'mail' => [ | ||
'class' => 'MailChannel', | ||
'from' => '[email protected]' | ||
] | ||
], | ||
], | ||
], | ||
] | ||
``` | ||
|
||
Validator | ||
Each notification class should implement NotificationInterface and contains a via method and a variable number of message building methods (such as `exportForMail`) that convert the notification to a message optimized for that particular channel. | ||
Example of notification that covers the case when an invoice has been paid: | ||
|
||
```php | ||
public function rules() | ||
use tuyakhov\notifications\NotificationInterface; | ||
|
||
class InvoicePaid implements NotificationInterface | ||
{ | ||
private $invoice; | ||
|
||
public function __cunstruct($invoice) | ||
{ | ||
$this->invoice = $invoice | ||
} | ||
|
||
public function exportForMail() { | ||
return Yii::createObject([ | ||
'class' => 'tuyakhov\notifications\messages\MailMessage', | ||
'view' => ['html' => 'invoice-paid'], | ||
'viewData' => [ | ||
'invoiceNumber' => $this->invoice->id, | ||
'amount' => $this->invoice->amount | ||
] | ||
]) | ||
} | ||
} | ||
``` | ||
|
||
You may use the NotifiableInterface and NotifiableTrait on any of your models: | ||
|
||
```php | ||
use yii\db\ActiveRecord; | ||
use tuyakhov\notifications\NotifiableTrait; | ||
use tuyakhov\notifications\NotifiableInterface; | ||
|
||
class User extends ActiveRecord implements NotifiableInterface | ||
{ | ||
use NotifiableTrait; | ||
|
||
public function routeNotificationForMail() | ||
{ | ||
return [ | ||
['notifications_code', CodeValidator::className()], | ||
]; | ||
return $this->email; | ||
} | ||
``` | ||
} | ||
``` |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters