未找到 Yii2 通知小部件类 [英] Yii2 notification widget class not found

查看:57
本文介绍了未找到 Yii2 通知小部件类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用machour"的通知小部件

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

但不知何故我什至不能在我的代码中调用 class: NotificationWidget

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

它说找不到类"

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

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

我尝试创建目录:后端/组件并尝试

i tried to create directory: backend/components and tried

使用后端/组件/通知;

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

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;

这是我调用时的代码:

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

?>

这是文件夹中的代码:backend/components:

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

推荐答案

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

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

例如:

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

    ];

并且您应该将后端(即高级模板)的引用更改为您正在使用的基本目录和命名空间..小心在此表单的扩展中指定正确的命名空间声明及其在 use 中的值 语句

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

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

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

并更改命名空间

namespace backend\components; 

 namespace app\models; 

通过这种方式,您可以将 Notification 类添加到您的模型中

in this way you add the Notification class to your Models

并将 use backend\components\Notification 更改为 app\models\Notification 也是您使用小部件的位置

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

这篇关于未找到 Yii2 通知小部件类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆