accessing application controllers from a module

Hi all,

How can I access application controller methods from inside a module? I know that for a model I can simply do "$user = new User()" within the module, how to access the controllers?


If you need to access some controller’s methods outside this controller, then you are probably doing something wrong. Controller is not a component to provide methods to other components, it’s just a thin layer between views and models. The solution can be to create a separate component which can be used by different modules and controllers or to put these methods inside the module class (controller can access it’s module’s methods easily).

You have to create the controller instance.

See createController

	function createControllerFromRoute($controllerRoute,&$actionId=null)


		$cResult = Yii::app()->createController($controllerRoute);

                //$cResult[0] -> the actionId, $cResult[1] -> the controller instance


		if (empty($cResult))		

			return null;


		$actionId = !empty($cResult[1]) ? $cResult[1] : null;

		if (empty($actionId))

			return null;

		return $cResult[0];


But using classes/components like andy_s told is better …

Thanks. I do want to do things right. The thing is, I have a "Item" model and LoadItem method inside ItemController which I use to calculate and join stuff. I am not sure the LoadItem method belongs to a model.

Now I am building an Admin interface and I need to load the item. So what should I do?

All calculations and joins should be inside a model. In the controller you should just call the model’s method with some parameters:

private function loadModel()


    return Item::model()->someMethod($_GET['id'));


And anywhere else you can do the same.

Thanks! I’ve give it a try.