2017-04-08 28 views
1

我试图用“machour的”通知窗口部件Yii2通知部件类

但不知何故,我甚至不能称之为类:NotificationWidget在我的代码

它说,“未找到类“

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

编辑:

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

使用后端/组件/通知;

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

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

更新你的问题,并添加您your_basic_app /配置/ WEB-PHP ..please – scaisEdge

+0

@scaisEdge我更新配置/网页代码 –

+0

@scaisEdge我更新了代码,这应该在Advance模板中工作。基本模板 –

回答

0

由于事实上,你正在使用基本的模板,你不应该添加配置元素在后台/配置(此是先进的模板) 但在你的应用程序目录/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 .. 
     ], 

    ]; 

,你应该参考与后端(即用于ANVANCED模板)更改为您的基本目录和中的命名空间使用..小心被你这种形式的扩展及其在use声明

值指定正确的命名空间声明

你应该将在后台/组件代码:在您的应用程序\型号\

和更改命名空间

namespace backend\components; 

namespace app\models; 
这样你通知类添加到您的模型

,改变use backend\components\Notificationapp\models\Notification也是您使用控件

+0

我实际上添加了模块到配置/网络 –

+0

一旦你有正确的配置,你必须调整正确的名称空间,并在你的代码中使用.. – scaisEdge

+0

你能更详细地描述它, 我需要使用'use'来调用代码,但哪个目录behide'使用' –