0

I need to check the records in the notifications table at every page load of every controller.

So I wrote it in a component and the component is executed in the bootstraping process.

I need the notifications to be available in the layout so that i can show them in the notification menu.

below is what I have tried so far:

component:

namespace admin\components;

use Yii;
use yii\base\Component;
use admin\models\Notification;

class NotificationManager extends \yii\base\Component{
  public function init() {

    $notifications = Notification::find()->orderBy('id DESC')->asArray()->all();
    //echo "<pre>"; print_r($notifications);exit;

    if(count($notifications)>0){
      foreach ($notifications as $notif) {
        if($notif['type'] == 'courier')
          $courier_notifications[] = $notif;
        elseif($notif['type'] == 'order')
          $order_notifications[] = $notif;
      }

      Yii::$app->view->params['courier_notifications'] = $courier_notifications;
      Yii::$app->view->params['order_notifications'] = $order_notifications;
    }
  }
}

Layout:

$courier_notifications = $this->params['courier_notifications'];

I am not sure which part am I going wrong: in component or in the layout? I appreciate your help.

4
  • Your layout gets rendered for all requests? ie, its the only layout ? Commented Nov 17, 2017 at 16:40
  • @ck_arjun yes, this is the only layout i have! Commented Nov 17, 2017 at 16:42
  • A widget would have done a better job. Anyway why cant you move the logic from init to a function inside component and access it directly in the layout ? Commented Nov 17, 2017 at 16:43
  • @ck_arjun I would appreciate a clue about how to do it as an answer! :) Commented Nov 17, 2017 at 16:46

2 Answers 2

1

Im not sure why your component execution during bootstrap fails to add the value to params.But believe it to be an overkill.

You can rather move the logic to component method and access in layout whenever necessary

Component.

namespace admin\components;

use Yii;
use yii\base\Component;
use admin\models\Notification;

class NotificationManager extends Component{

  public function notifications($type = 'courier') {
     $notifications = Notification::find()
            ->where(['type' => $type])
            ->orderBy('id DESC')
            ->asArray()->all();
     return $notifications;
  }
}

Add the component class under Components section in your config file

'notificationManager ' => [
        'class' => 'admin\components\NotificationManager'
 ]

Layout

$courier_notifications = yii::$app->notificationManager->notifications('courier');
Sign up to request clarification or add additional context in comments.

Comments

1

If you really want to go bootstrap mode, you need to implement yii\base\BootstrapInterface and put your logic in the bootstrap($app) method in order for the param to be available site-wide by setting the value of Yii::$app->params['notifications'] to the result of your logic.

Another common approach is to add a new method public function displayNotifications or whatever you want to name it, to your component, move all the logic in it and then in your layout/view etc., call it with Yii::$app->notificationManager->displayNotifications(). You can also pass additional parameters to it and enhance your logic.

notificationManager has to be replaced with the name you registered your custom component in the Yii app config (web.php for basic app, main.php for advanced app).

LE - If you only registered your component for bootstrap, you should also register it in the components array.

'notificationManager' => [ 'class' => '\admin\components\NotificationManager' ]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.