deviloper
deviloper

Reputation: 7240

accessing an array in a component from the layout in yii2

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.

Upvotes: 0

Views: 183

Answers (2)

Ravenous
Ravenous

Reputation: 1160

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' ]

Upvotes: 1

ck_arjun
ck_arjun

Reputation: 1417

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');

Upvotes: 1

Related Questions