0

i have tried to access current controller and action name using following code , it works for the controller but failing in case of action name (code location: protected/components/controller.php)

class Controller extends CController
{

    public $layout='//layouts/column1';

    public $menu=array();

    public $breadcrumbs=array();
        public function init()
        {
           echo Yii::app()->controller->id; // working fine
          echo Yii::app()->controller->action->id; exit; // showing error ![enter image description here][1]

        }
}
Deepu Thomas
  • 43
  • 1
  • 2
  • 10

1 Answers1

0

Use -

protected function beforeAction($event) {
    echo Yii::app()->controller->id;
    echo Yii::app()->controller->action->id;
    exit;
}

rather than

public function init() {
    echo Yii::app()->controller->id;
    echo Yii::app()->controller->action->id;
    exit;
}

In init() runs when the controller is initialized, but the action is not known yet and so the object does not have the action property associated with it.

Kunal Dethe
  • 1,254
  • 1
  • 18
  • 38