Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yii2 - Get Current Action ID in main.php

Tags:

yii2

In the file main.php at views/layouts/main.php, I want to add few classes to the body tag and add css class 'active' to the navigation bar items according to the view being displayed. Is there any way I can get action id in main.php?

I hope that html is not req

like image 820
Omar Tariq Avatar asked Feb 27 '15 16:02

Omar Tariq


Video Answer


2 Answers

Or you can use:

Yii::$app->controller->id // get controller name

to access the controller, and:

Yii::$app->controller->action->id // get action name

for getting the current action id. But put this directive at the top of file:

use Yii;  
like image 192
Hemant Sharma Avatar answered Sep 28 '22 00:09

Hemant Sharma


In a layout you can access the current controller via $this->context, and so the current action id by $this->context->action->id

like image 41
Joe Miller Avatar answered Sep 27 '22 22:09

Joe Miller