Extending CController

Hey guys,

I’d like to configure a MainController to extend init() method to all my controller classes inside application.

Where Am I expected do configure it if I want it to be loaded when I access modules controllers?

I tried putting it inside main controller app path however it brings me include MainController not found error.

Some idea? Do I need use Yii::import() method in scope of each CWebmodule extended class?

Does exist some better way to do it?

I think it is better to modify the beforeControllerAction method inside your xxxModule.php to do this.

Instead, try to put your MainController at ‘extensions’ dir.

You know, I can do it in CakePHP just creating a DefaulController or something like that in app root. I can extend it to all my app controllers with no problem.

Do you think its a wrong way to do it?

:D thank you!

it can be done easily in Yii also. just create your MainController.php inside components directory


class MainController extends CController 

{ 

  public function init() 

  { 

    parent::init(); 

    // add your code for init here 

  } 

}

and inside your controllers you can simply extend MainController


class SiteController extends MainController

{

 // class contents

}

you can also configure Yii so you can put your MainController.php inside the dir protected/controllers and then extending it with classes in the same dir by modifying your protected/config/main.php files as follows




.................

	// autoloading model and component classes

	'import'=>array(

		'application.models.*',

		'application.components.*',

		'application.controllers.*', // added so class will be autoloaded from protected/controllers

	),

.................



what i did there was add this line




		'application.controllers.*',

to what was generated in the config file by default.

Thank you! :)

I was expected to use it as a component because my application is structured by modules.

I think it won’t be too smart extending MainController (a root app controller) in module controllers (has own way to work) .

But this example is very important for other things I am gonna do! :) Thank you. I have no more doubts.