Yii2 Get the current action in the controller

How can I get the current action?

This code:

if (!Yii::$app->controller->action->id == 'lang') { Url::remember(); } 

returns an error:

PHP Note - yii \ base \ ErrorException

Trying to get a non-object property

+5
source share
3 answers

You should use beforeAction () instead of init() .

You can also just use $this because it contains the current controller.

 public function beforeAction($action) { if (parent::beforeAction($action)) { if ($this->action->id == 'lang') { Url::remember(); } return true; // or false if needed } else { return false; } } 
+7
source

if you use Yii2 - try the following: $this->context->action->id;

+8
source

You can get the current action id :)

  Yii::$app->controller->id; 
0
source

Source: https://habr.com/ru/post/1212766/


All Articles