找不到Yii2通知窗口小部件类

时间:2017-04-08 06:10:10

标签: notifications yii2 widget yii2-basic-app

我试图使用" machour"通知小部件

但不知怎的,我甚至无法在我的代码中调用class:NotificationWidget

它表示" Class未找到"

我使用yii2-basic-template,如何调用该类?

编辑:

我尝试创建目录:backend / components并尝试

使用backend / components / Notification;

但我使用的是基本模板,所以我不认为它是对的

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;

这是我称之为的代码:

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

?>

这是文件夹中的代码: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/';
        };
    }

}

这些代码适用于高级模板,我认为我需要更改某些内容以使其适用于基本模板

1 个答案:

答案 0 :(得分:0)

由于您使用的是基本模板,因此您应该添加不在backend / config中的配置元素(这适用于高级模板) 但是在你的应用程序目录/config/web.php

例如:

      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 ..
        ],

    ];

并且您应该将对后端(对于高级模板)的引用更改为您使用的基本目录和命名空间。请注意在此表单的扩展中指定正确的命名空间声明及其在{{1}中的值}陈述

您应该在后端/组件中移动代码:在app \ models \

并更改命名空间

use

namespace backend\components; 

以这种方式将Notification类添加到模型

并将 namespace app\models; 更改为use backend\components\Notification,同时使用小部件