5

I want to call a function in another controller from the controller.

class FirstController extends Controller {
      public function test1() { return 'OK'; }
}
class SecondController extends Controller {
      public function callTest1() { First::test1(); }
}

--> server error
Help me resolve it.

8 Answers 8

9

You cannot call that method directly because it's not static, so you 'd have to create an instance of FirstController first. When you want to do that, use CWebApplication::createController:

// supply appropriate route in place of 'first/index'
list($first) = Yii::app()->createController('first/index');
$first->test1();

However, there shouldn't be a need to call methods from another controller; this is a bad code smell. Perhaps it would be more appropriate to refactor your code and pull it out of the controller (maybe into a model).

Sign up to request clarification or add additional context in comments.

2 Comments

I do it, but it still error. If we need to handle something which are decleared in a function of another controller, what should we do to handle it [ in current controller ], without call function of another controller. Could u show me what better to do, to change my rigid mind. thank u so much
@HuyTran, You may add in components/controller.php method witch will call from second/index and first/index
1

The word 'Controller' should not be used when calling createController() and you should take [0] element of the result, before calling functions

 $process = Yii::app()->createController('First'); //create instance of FirstController
 $process=$process[0];
 $process->test1(); //call function 

Comments

1

You can also use Yii::$app->runAction().

Comments

1

In Yii2 basic application following component will be use to call action

Yii::$app->runAction('controllername/create', $params);

Comments

1

Instead of using createController which creates an instance based on a route, when calling via code, it's probably best to use createControllerByID.

    $first = Yii::$app->createControllerByID('first');
    $first->test1();

Make sure test1 is public function

Comments

0

You cannot call directly your function: first create instance of controller, then call function:

$process = Yii::app()->createController('FirstController'); //create instance of controller

$process->test1(); //call function 

Comments

0

If method test1 in your FirstController is not using $this you can simply make it static. Instead of:

public function test1() { return 'OK'; }

sign it as:

public static function test1() { return 'OK'; }

Then you can call it from everywhere:

FirstController::test1();

It all depends on your needs...

Comments

0

This worked for me,

for PatientsController and method printPublicReport() with $params = $patientId, $appId, $testIds

    list($patient) = Yii::$app->createController('patients/print-public-report', $postDataAry); 

    $patient->printPublicReport($patientId, $appId, $testIds);

2 Comments

While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value.
@Tyler2P ok thanks I'll take care of this from now on

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.