Call another controller's action


I have one question:

Is it possible to call another controlerr’s action from actual controller?



class actualControler extends CController {

public function actionOne() {

$fromAnother = <here I want to call anoterAction>;




class anotherController extends CController {

public function anotherAction() {

// do something and return it




I want to call anoterController anotherAction from actualController actionOne

what do you think? is it possible?

both of controllers are in other files, of course :wink:


You should not do that. Use widgets or components.

1 Like

but it could be usefull … never mind. :mellow:

you can build class-action to realize your requirement

what about using:




In 1.1, you can use CController::forward().

but CController::forward() won’t return the result of called method…


maybe not nice but working is to pass data from called method through Yii::app()->user->set/getState(); or set/getFlash().

If you want a method/action that is called from different places/controllers, you may use a static model-method or a static controller-method.

yes. but then i have to do as yugene wrote, Yii::import() controller class, i was thinking if that can be tackled someway.

Ok, after some considerations i think that importing class will be faster than forwarding to specific route.

You could import the whole controllers folder in config if needed.

But I guess you’ll be better when you put that method into a model or a special component since the controller-action you want to call obviously doesn’t function as a real controller-action.

class ServiceController extends Controller


	public function actionIndex()


		Yii::import('application.controllers.back.ConsolidateController'); // ConsolidateController is another controller in back controller folder

		echo ConsolidateController::test(); // test is action in ConsolidateController

class ServiceController extends Controller


	public function actionIndex()



	$obj =new CservicesController(); // preparing object

	echo $obj->test(); exit; // calling test class of ServiceController 

If you are going to utilize an action of another controller, the best thing is CController::forward().

It is a wrapper around Yii::app()->runController.

Or you can directly call like this.

Yii::app()->runController('catagory/index');  //catagory is the controller id;index is action id.


If you want to use methods of of another controller, we can do get the instance like this.

$cat=Yii::app()->createController('catagory');//returns array containing controller instance and action index.

$cat=$cat[0]; //get the controller instance.

$cat->loadModel(1); //use a public method.

1 Like

Or using the action() method inside Controllers to map certain action to certan file, if it is for actions accesibles from the web. But that way the controller will have the same action name, for example, action is named ‘actionDeletePost’ you can use it in AdminController and in UserController, appending /deletePost to the URL.

In my Controller A

class Controller A extends Controller


    public function action save()


       &#036;array1 = &#036;_POST['array'];

           &#036;A = &#036;this-&gt;redirect(array('controller B/insert','data'=&gt;&#036;array1));

           echo &#036;a;



In My controller B

class Controller B extends Controller


    public function action Insert(&#036;data)


        echo &#036;data;


        return value;



I am newbie in yii framework. I have problem with, call controller b action Insert() from controller a with parameters. But i have error like &quot;400 Your request is invalid.&quot;. i don't know  how to fix it. please help me anyone..&#33; Sorry for my English.. Thank You..

Add your action names in access rules…

How can I do it in Yii2?

Hello Everyone

This problem can be resolve by using createController method like this

[color="#006400"]$cc = Yii::app()->createController(anothercontroller);

$cc[0]->actionIndex();[/color] //actionIndex is the action of anothercontroller

for eg…

// First controller from you want to call another controller’s action

[color="#006400"]class SiteController extends CController


function actionIndex(){

   &#036;cc = Yii::app()-&gt;createController(user);




//User controller contains actionIndex. You can call any action in place of actionIndex.

[color="#006400"]class UserController extends CController{

function actionIndex(){ 

        echo 'hello World&#33;';



[color="#FF0000"]Note:- Just be sure that you have defined controller’s name globally.[/color]

Hello every one

This problem can be resolve by using:

public function actionCreate()


    if (Yii::&#036;app-&gt;user-&gt;isGuest) {


        return Yii::&#036;app-&gt;runAction('site/login'); //calling login action in site Controller for showing login form to the user