Call each other between Yii methods

1. Call the actions of different controllers under the same module

// 在frontend\controllers\TestController\actionTest 方法中调用同一模块下的
// frontend\controllers\NewController\actionNewAction 方法
Yii::$app->runAction('new/new-action');

Two, call actions under different modules

// 在frontend\controllers\TestController\actionTest 方法中调用另一个模块下的
// backend\controllers\NewController\actionNewAction 方法
return (new NewController($this->id, $this->module))->runAction('new-action');

Three, call the controller action in the model class

class MyModel extends \yii\base\Model{
    public function modelfun{
       //调用控制器中的静态方法fun1()
       \app\controllers\MyController::fun1();
       //调用控制器中的实例方法fun2():先实例化,再调用其方法,不过一般没人这么干!
       $control=new \app\controllers\MyController();
       $control->fun2();
    }
}

Guess you like

Origin blog.csdn.net/weixin_43272542/article/details/114028968