Yii2 notification widget class not found

不羁岁月 提交于 2019-12-14 02:22:18

问题


I'm trying to use "machour's" notification widget

But somehow I can't even call class: NotificationWidget in my code

It says that "Class is not found"

I'm using yii2-basic-template, how can I call that Class ?

edit:

i tried to create directory: backend/components and tried

use backend/components/Notification;

But i use basic template so i don't think it's right

https://github.com/machour/yii2-notifications

<?php

$params = require(__DIR__ . '/params.php');

$config = [
        'language' => 'en',
        'bootstrap' => ['languagepicker'],
        'modules' => [
            'notifications' => [
                'class' => 'machour\yii2\notifications\NotificationsModule',
                // Point this to your own Notification class
                // See the "Declaring your notifications" section below
                'notificationClass' => 'app\models\Notification',
                // Allow to have notification with same (user_id, key, key_id)
                // Default to FALSE
                'allowDuplicate' => false,
                // This callable should return your logged in user Id
                'userId' => function() {
                    return \Yii::$app->user->id;
                }
            ],
          'redactor' => 'yii\redactor\RedactorModule',
          'class' => 'yii\redactor\RedactorModule',
          'uploadDir' => '@webroot/uploads',
          'uploadUrl' => '../web/uploads',
      ],
    'id' => 'basic',
    'basePath' => dirname(__DIR__),
    'bootstrap' => ['log'],
    'components' => [
          'languagepicker' => [
            'class' => 'lajax\languagepicker\Component',
            'languages' => ['en-US', 'vi']                   // List of available languages (icons only)
        ],
        'request' => [
            // !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
            'cookieValidationKey' => 'hhs9xband',
        ],
        'cache' => [
            'class' => 'yii\caching\FileCache',
        ],
        'user' => [
            'identityClass' => 'app\models\User',
            'enableAutoLogin' => true,
        ],
        'errorHandler' => [
            'errorAction' => 'site/error',
        ],
        'mailer' => [
            'class' => 'yii\swiftmailer\Mailer',
            // send all mails to a file by default. You have to set
            // 'useFileTransport' to false and configure a transport
            // for the mailer to send real emails.
            'useFileTransport' => false,
            'transport' => [
                'class' => 'Swift_SmtpTransport',
                'host' => '',
                'username' => '',
                'password' => '',
                'port' => '587',
            ],
        ],
        'log' => [
            'traceLevel' => YII_DEBUG ? 3 : 0,
            'targets' => [
                [
                    'class' => 'yii\log\FileTarget',
                    'levels' => ['error', 'warning'],
                ],
            ],
        ],
        'db' => require(__DIR__ . '/db.php'),
        /*
        'urlManager' => [
            'enablePrettyUrl' => true,
            'showScriptName' => false,
            'rules' => [
            ],
        ],
        */
    ],
    'params' => $params,
];

if (YII_ENV_DEV) {
    // configuration adjustments for 'dev' environment
    $config['bootstrap'][] = 'debug';
    $config['modules']['debug'] = [
        'class' => 'yii\debug\Module',
        // uncomment the following to add your IP if you are not connecting from localhost.
        //'allowedIPs' => ['127.0.0.1', '::1'],
    ];

    $config['bootstrap'][] = 'gii';
    $config['modules']['gii'] = [
        'class' => 'yii\gii\Module',
        // uncomment the following to add your IP if you are not connecting from localhost.
        //'allowedIPs' => ['127.0.0.1', '::1'],
    ];
}

return $config;

This is the code when i call it:

use backend\components\Notification
<?= NotificationsWidget::widget([
    'theme' => NotificationsWidget::THEME_GROWL,
    'clientOptions' => [
        'location' => 'br',
    ],
    'counters' => [
        '.notifications-header-count',
        '.notifications-icon-count'
    ],
    'listSelector' => '#notifications',
]);

?>

And this is the code inside folder: backend/components:

namespace backend\components;

use Yii;
use common\models\Meeting;
use common\models\Message;
use machour\yii2\notifications\models\Notification as BaseNotification;

class Notification extends BaseNotification
{

    /**
     * A new message notification
     */
    const KEY_NEW_MESSAGE = 'new_message';
    /**
     * A meeting reminder notification
     */
    const KEY_MEETING_REMINDER = 'meeting_reminder';
    /**
     * No disk space left !
     */
    const KEY_NO_DISK_SPACE = 'no_disk_space';

    /**
     * @var array Holds all usable notifications
     */
    public static $keys = [
        self::KEY_NEW_MESSAGE,
        self::KEY_MEETING_REMINDER,
        self::KEY_NO_DISK_SPACE,
    ];

    /**
     * @inheritdoc
     */
    public function getTitle()
    {
        switch ($this->key) {
            case self::KEY_MEETING_REMINDER:
                return Yii::t('app', 'Meeting reminder');

            case self::KEY_NEW_MESSAGE:
                return Yii::t('app', 'You got a new message');

            case self::KEY_NO_DISK_SPACE:
                return Yii::t('app', 'No disk space left');
        }
    }

    /**
     * @inheritdoc
     */
    public function getDescription()
    {
        switch ($this->key) {
            case self::KEY_MEETING_REMINDER:
                $meeting = Meeting::findOne($this->key_id);
                return Yii::t('app', 'You are meeting with {customer}', [
                    'customer' => $meeting->customer->name
                ]);

            case self::KEY_NEW_MESSAGE:
                $message = Message::findOne($this->key_id);
                return Yii::t('app', '{customer} sent you a message', [
                    'customer' => $meeting->customer->name
                ]);

            case self::KEY_NO_DISK_SPACE:
                // We don't have a key_id here
                return 'Please buy more space immediately';
        }
    }

    /**
     * @inheritdoc
     */
    public function getRoute()
    {
        switch ($this->key) {
            case self::KEY_MEETING_REMINDER:
                return ['meeting', 'id' => $this->key_id];

            case self::KEY_NEW_MESSAGE:
                return ['message/read', 'id' => $this->key_id];

            case self::KEY_NO_DISK_SPACE:
                return 'https://aws.amazon.com/';
        };
    }

}

Those codes are for Advance template, i think i need to change something to make it works on Basic template


回答1:


Due the fact you are using basic template you should add the configuration elements not in backend/config (this is for advanced template) but in you application directory /config/web.php

eg:

      config = [
        'id' => 'your_app_id',
        'basePath' => dirname(__DIR__),
        'bootstrap' => ['log'],
        'components' => [
             ......
        ],
        // ...
        'modules' => [
            'notifications' => [
                'class' => 'machour\yii2\notifications\NotificationsModule',
                // Point this to your own Notification class
                // See the "Declaring your notifications" section below
                'notificationClass' => 'app\models\Notification',
                // Allow to have notification with same (user_id, key, key_id)
                // Default to FALSE
                'allowDuplicate' => false,
                // This callable should return your logged in user Id
                'userId' => function() {
                    return \Yii::$app->user->id;
                }
            ],
            // your other modules ..
        ],

    ];

and you should change the reference to backend (that is for anvanced template) to you basic directory and namespace in use .. be careful to specify the correct namespace declaration in your extension of this form and its value in use statements

you should move your code in backend/components: in your app\models\

and change the namespace

namespace backend\components; 

to

 namespace app\models; 

in this way you add the Notification class to your Models

and change the use backend\components\Notification to app\models\Notification also where you use the widget



来源:https://stackoverflow.com/questions/43290812/yii2-notification-widget-class-not-found

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!