Chux
Chux

Reputation: 1227

Extending main controller (or view) in Yii2

I have worked using Yii1 in a lot of projects. In most of them, we had a property in the main controller called $bodyClass, which we used in the views as $this->bodyClass.

In Yii2, $this references to the view instead of the controller. And also, the base controller used by all the Yii2 controllers is not on the app, but in the yii folder itself.

What is the best way of accomplish what I'm trying to do? a) Creating my own components/BaseController. All my controller will inherit from it, and I will access to the properties in the views as $this->context->property b) Extending the ViewComponent

Advantages and disadvantages? Or is my approach completely wrong?

Upvotes: 0

Views: 222

Answers (1)

The View class has a property named $params, so you can pass any variable in the controller to the View like this:

$this->view->params['param'] = 'value';

Accessing in the view

/* @var $this yii\web\View */

echo $this->params['param'];

Upvotes: 0

Related Questions