从自定义控制器类继承-未找到


Yii - Inheriting From Custom Controller Class - Not Found

class SomeController extends Controller
{
        public function actionIndex() {
                echo 'This is some controller';
        }
}

class AnotherController extends SomeController
{
        public function actionIndex() {
                echo 'This is another controller';
        }
}

如此:

index.php?r=some

但是…

index.php?r=another

说:

PHP警告

include(someecontroller .php): failed to open stream: No such file or directory

两个文件都在

test'protected'controllers'

顺便说一句,在过去我也试过使用Gii控制器生成器" someecontroller "作为基类…

它说:

The controller has been generated successfully. You may try it now.
Generating code using template 
"C:'xampp'htdocs'yii'framework'gii'generators'controller'templates'default"...
generated controllers'YetAnotherController.php
generated views'yetAnother'index.php
done!

当我点击"try it now"它还说:

PHP警告

include(someecontroller .php): failed to open stream: No such file or directory

Edit:

protected/controllers中的类不会自动加载,因此你必须先导入父类文件,然后再从它扩展:

在<<p> em> AnotherController.php :
Yii::import('application.controllers.SomeController');
public class AnotherController extends SomeController {
    // ...
}

如果你也需要从url访问基类,你可以使用上面的方法。否则,你可以把你的基类放在protected/components中,就像你已经发现的那样。


Yii自动加载仅在文件的名称与文件包含的类名称相同时才有效。这意味着class SomeController应该在SomeController.php文件中。

做这些修改,它应该工作。

一个有用的wiki:理解自动加载Helper类和Helper函数。

指南的链接:

类文件应该以它们包含的公共类命名。

要扩展任何类,只需进入配置文件并在导入部分添加类

'import' => array('application.controllers.SomeController')

这将使它在整个应用程序中可用,而无需显式导入。