Yii2 creating global dynamic values for all application's actions

895 Views Asked by At

I want to pass current path info, i.e controller/action/params, to a widget that uses another model to generate some static HTML contents in the views of the application, including the layout too.

In-order to achieve that goal, I have defined the following controller AppController and I make all of my app controllers extends it instead of yii\web\Controller;

<?php
namespace app\controllers;
use Yii;
use yii\web\Controller;

class AppController extends Controller
{
  public function __construct($id, $module, $config = array()) {
    parent::__construct($id, $module, $config);
    $this->view->params['path2'] = Yii::$app->controller->id;
  }  
  /*
  public function beforeAction($action) {
    parent::beforeAction($action);
    $this->view->params['path2'] = Yii::$app->controller->id;
  }  */

}

And, for example, in SiteController:

class SiteController extends AppController
{...

The problem is in any view of any SiteController action, for eaxmple about echo $this->params['path2'] returns blank while I expect it to echo site. In addition, uncommenting beforeAction makes all SiteController actions produces blank pages.

I need any way to allow, globally, set $this->params['path2'] value to be the current controller/action/params regardless what routs rewrites is.

0

There are 0 best solutions below