PHP code example of deadbyback / yii2-notifications
1. Go to this page and download the library: Download deadbyback/yii2-notifications library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
deadbyback / yii2-notifications example snippets
$notifier = new \tuyakhov\notifications\Notifier([
'channels' => [...],
]);
$notifier->send($recipients, $notifications);
$recipient = User::findOne(1);
$notification = new InvoicePaid($invoice);
Yii::$app->notifier->send($recipient, $notification);
use tuyakhov\notifications\NotificationInterface;
use tuyakhov\notifications\NotificationTrait;
class InvoicePaid implements NotificationInterface
{
use NotificationTrait;
private $invoice;
public function __construct($invoice)
{
$this->invoice = $invoice;
}
/**
* Prepares notification for 'mail' channel
*/
public function exportForMail() {
return Yii::createObject([
'class' => '\tuyakhov\notifications\messages\MailMessage',
'view' => ['html' => 'invoice-paid'],
'viewData' => [
'invoiceNumber' => $this->invoice->id,
'amount' => $this->invoice->amount
]
])
}
/**
* Prepares notification for 'sms' channel
*/
public function exportForSms()
{
return \Yii::createObject([
'class' => '\tuyakhov\notifications\messages\SmsMessage',
'text' => "Your invoice #{$this->invoice->id} has been paid"
]);
}
/**
* Prepares notification for 'database' channel
*/
public function exportForDatabase()
{
return \Yii::createObject([
'class' => '\tuyakhov\notifications\messages\DatabaseChannel',
'subject' => "Invoice has been paid",
'body' => "Your invoice #{$this->invoice->id} has been paid",
'data' => [
'actionUrl' => ['href' => '/invoice/123/view', 'label' => 'View Details']
]
]);
}
}
use yii\db\ActiveRecord;
use tuyakhov\notifications\NotifiableTrait;
use tuyakhov\notifications\NotifiableInterface;
class User extends ActiveRecord implements NotifiableInterface
{
use NotifiableTrait;
public function routeNotificationForMail()
{
return $this->email;
}
}
$model = User::findOne(1);
foreach($model->unreadNotifications as $notification) {
$notification->markAsRead();
// the following methods are also available
$notification->markAsUnread();
$notification->isUnread();
$notification->isRead();
}
php composer.phar
php yii migrate/up
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.