php 在 Yii 的另一个控制器中调用函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4022483/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Call function in another controller in Yii
提问by ZhukovRA
I've created 2 controllers in my Yii application: FirstController.php and SecondController.php in default controller path.
我在 Yii 应用程序中创建了 2 个控制器:默认控制器路径中的 FirstController.php 和 SecondController.php。
FirstController.php:
FirstController.php:
<?php
class FirstController extends Controller {
public static function returnFunc() { return 'OK'; }
}
SecondController.php:
第二控制器.php:
<?php
class SecondController extends Controller {
public function exampleFunc() {
$var = First::returnFunc();
}
}
When I try to execute exampleFunc()
in SecondController, Yii throw the error:
当我尝试exampleFunc()
在 SecondController 中执行时,Yii 抛出错误:
YiiBase::include(FirstController.php) [<a href='function.YiiBase-include'>function.YiiBase-include</a>]: failed to open stream: No such file or directory
Calling FirstController::returnFunc()
similarly don't work.
调用FirstController::returnFunc()
同样不起作用。
I'm newbee in OOP and Yii framework. What's the problem?
我是 OOP 和 Yii 框架的新手。有什么问题?
回答by ZhukovRA
I've solved this problem. The autoloader doesn't load controllers.
我已经解决了这个问题。自动加载器不加载控制器。
It was in config/main.php
:
它在config/main.php
:
'import' => array(
'application.models.*',
'application.components.*',
),
All work with this:
一切都与此有关:
'import' => array(
'application.models.*',
'application.components.*',
'application.controllers.*',
),
回答by Kiran
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()
{
Yii::import('application.controllers.back.CservicesController');
$obj =new CservicesController(); // preparing object
echo $obj->test(); exit; // calling method of CservicesController
回答by Philip Walton
When you create a Yii project, each of your controllers extend the Controller class, and that class extends the built in Yii class CController.
当你创建一个 Yii 项目时,你的每个控制器都扩展了 Controller 类,并且该类扩展了 Yii 的内置类 CController。
This is nice because Controller is a class within your application (it can be found in the components folder).
这很好,因为 Controller 是应用程序中的一个类(它可以在 components 文件夹中找到)。
If you want a method to be accessible by both of your controllers, put that method in the Controller class, and since they both extend it. They'll both have access. Just make sure to declare it either public or protected.
如果您希望两个控制器都可以访问一个方法,请将该方法放在 Controller 类中,因为它们都扩展了它。他们都可以访问。只需确保将其声明为 public 或 protected。