Learning the ropes of Yii.
On cakephp, they have this app_controller, app_model files before they load other controllers. I was wondering, if there is an equivalent on Yii.
On cake, they have this beforeFilter() as well that we can add up on app_controller, to set logic settings before anything else. I found out that Yii has beforeAction().
http://www.yiiframework.com/doc/api/1.1/CController#beforeAction-detail
My understanding is it will be placed on each controller, is there a way to add it like a global method so, I won’t be adding it on all controller?
Thanks. Sorry, starting newbie on Yii.
kokomo
(Mwerlberger85)
October 30, 2012, 7:01am
2
Welcome to the forum
Add beforeAction method to "base" controller (e.g. protected/components/Controller.php) from which all other Controllers should inherit from.
Thanks. Worked perfectly.
I just like to note that you still need to do on every controller.
public function beforeAction() {
parent::beforeAction();
}
If you want to inherit some settings from the parent controller.
mentel
(Yii)
November 9, 2012, 10:20am
4
You can also take a look at CApplication::onBeginRequest.
I actually had an issue when I implemented this.
The methods on the site controllers are not being read.
Basically…
components/Controller.php
public function beforeAction() {
echo "Component, Controller";
}
controller/SiteController.php
public function beforeAction() {
parent::beforeAction();
}
Like on the SiteController, actionIndex()
It won’t be read anymore. Did I missed something?
Thanks
Got it, the parent should return true.
protected function beforeAction($action)
{
return true;
}
See: http://www.yiiframework.com/doc/api/1.1/CController#beforeAction-detail