English 中文(简体)
Yii 控制器另一个控制器的调用功能
原标题:Call function in another controller from a controller in Yii

我要从控制器中调用另一个控制器中的函数。

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

--> server error
Help me resolve it.

问题回答

您不能直接调用该方法, 因为它不是 stistic , 所以您必须先创建一个 Firstcurrent 实例。 如果您想要这样做, 请使用 < a href=" http://www. yiiframework. com/doc/ application/ application# create constor- detail"\ code > Code > Code > CWEBApplication::: createcudy :

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

然而,没有必要从另一个控制器调用方法;这是一个糟糕的代码气味。 也许重构您的代码并把它从控制器中拉出来(也许可以变成一个模型)更为合适。

创建主计长 () 时不应使用主计长一词, 您在调用功能前应该先使用结果的 [ 0] 元素

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

您也可以使用 Yii::$app- gt;run Action ()

在Yii2中,以下组成部分的基本应用将用于号召行动

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

而不是使用创建控制器来创建基于路径的实例, 当通过代码调用时, 最好使用创建控制器ByID 。

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

确保测试1 是公共功能

您不能直接调用您的函数: 首先创建控制器实例, 然后调用函数 :

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

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

如果您的 test1 在您的 Firstcurrent 中的方法没有使用 $ this 您可以简单地将其设置为 stistic 。 而不是 :

public function test1() { return  OK ; }

标记为:

public static function test1() { return  OK ; }

然后,你可以到处叫它:

FirstController::test1();

这都取决于你的需要...

这对我管用

以1美元=1美元/美元/美元/美元/美元/美元/美元/美元/美元/美元/美元/美元/美元/美元/美元/美元/美元/美元/美元/美元/美元/美元/美元/美元/美元/美元/美元/美元/美元/美元/美元/美元/美元/美元/美元/美元/美元/美元/美元/美元/美元/美元/美元/美元/美元/美元/美元/美元/美元/美元/美元/美元/美元/美元/美元/美元/美元/美元/美元/美元/美元/美元/美元/美元/美元/美元/美元/美元/美元/美元/美元/美元/美元/美元/美元/美元/美元/美元/美元/美元/美元/美元/美元/美元/美元/美元/美元/美元/美元/美元/美元/美元/

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

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




相关问题
uninitialized constant

I m trying to do this tutorial-> http://netbeans.org/kb/docs/ruby/rapid-ruby-weblog.html BUT its giving me this error: NameError in PostsController#index uninitialized constant PostsController::...

Rails - Too much logic in views?

I have an application used by several organizations and I want to check that users of one domain (a.domain.com) cannot edit users of another domain (b.domain.com). My question is where to put the ...

Controller specs in isolation mode and render :update

I am using RSpec for writing my controller tests/specs. I faced the problem, that the following code gets rendered: render :update do |page| page[ middle_content ].replace_html :partial => "...

registering event listeners in a controller

I m working on creating a simple MCV application, in order to understand MVC better. The problem I m having is registering event listeners. The way I see MVC is that the view dispatches events, the ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

热门标签