YII 2 NOTIFICATION PART 1

 

Posted on 

Check details here.

  1. Add
"webzop/yii2-notifications": "*"

to the require section of your composer.json file.

2. Configure

[
    'modules' => [
        'notifications' => [
            'class' => 'webzop\notifications\Module',
            'channels' => [
                'screen' => [
                    'class' => 'webzop\notifications\channels\ScreenChannel',
                ],
                'email' => [
                    'class' => 'webzop\notifications\channels\EmailChannel',
                    'message' => [
                        'from' => 'example@email.com'
                    ],
                ],
            ],
        ],
    ],
]

Create notifications/Account/Notificstion.php

<?php

namespace backend\notifications;

use Yii;
use webzop\notifications\Notification;

class AccountNotification extends Notification
{
 const KEY_NEW_ACCOUNT = 'new_account';

const KEY_RESET_PASSWORD = 'reset_password';

/**
 * @var \yii\web\User the user object
 */
 public $user;

/**
 * @inheritdoc
 */
 public function getTitle(){
 switch($this->key){
 case self::KEY_NEW_ACCOUNT:
 return Yii::t('app', 'New account {users} created', ['users' => '#'.$this->user->id]);
 case self::KEY_RESET_PASSWORD:
 return Yii::t('app', 'Instructions to reset the password');
 }
 }

/**
 * @inheritdoc
 */
 public function getRoute(){
 return ['/users/edit', 'id' => $this->user->id];
 }
}
?>

Add function  in Controller

public function actionNotif(){

$user = User::find()->where(['id'=>2])->one();

AccountNotification::create(AccountNotification::KEY_RESET_PASSWORD, ['user' =>$user])->send();
 return $this->render('index');

}

Yii Migrate

yii migrate/up --migrationPath=vendor/webzop/yii2-notifications/migrations/

Add in menu items in main

 if (Yii::$app->user->identity->username == "verna@yahoo"){
 $menuItems[] = \webzop\notifications\widgets\Notifications::widget(); 
 }

Under webzop/yii2-notifications/Notification.php

namespace webzop\notifications;

use Yii;

/**
 * This is the base class for a notification.
 *
 * @property string $key
 * @property integer $userId
 * @property array $data
 */
abstract class Notification extends \yii\base\BaseObject
{
 public $key;

public $userId = 2;

public $data = [];

Screenshot

yii2-notif

Post a Comment

0 Comments