How to set the title value from the controller ? Yii2

  • one
    And what in view does not suit? - Alexey Shimansky
  • And what does not suit $this->title ? - Andrey Kolomensky
  • @ Onedev.Link for some reason does not work. - Mr. Music

2 answers 2

As far as I know, in yii2 they refused to set the title in the controller and transferred this action to the view .

In a view, you can do this: $this->title = "Мой заголовок";

If you really want to set the title in the controller, you will most likely have to define a variable in it to write the value to and transfer it to the view:

 $title = 'Мой заголовок'; return $this->render('myPage', ['title' => $title]); 

And in view write:

$this->title = $title;

     class SiteController extends Controller { public function actionIndex() { $this->getView()->title = 'Мой заголовок'; return $this->render('index'); } }