class SomeController extends Controller
{
public function actionIndex() {
echo 'This is some controller';
}
}
class AnotherController extends SomeController
{
public function actionIndex() {
echo 'This is another controller';
}
}
Run Code Online (Sandbox Code Playgroud)
这有效:
index.php?r=some
Run Code Online (Sandbox Code Playgroud)
但......
index.php?r=another
Run Code Online (Sandbox Code Playgroud)
说:
PHP警告
include(SomeController.php):无法打开流:没有这样的文件或目录
这两个文件都在
test\protected\controllers\
Run Code Online (Sandbox Code Playgroud)
BTW过去我也尝试使用Gii Controller Generator和"SomeController"作为基类......
它说:
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!
Run Code Online (Sandbox Code Playgroud)
当我点击"立即尝试"时,它还说:
PHP警告
include(SomeController.php):无法打开流:没有这样的文件或目录
boo*_*dev 12
编辑:
在类保护/控制器不能自动加载,因此你必须从其延伸之前导入的父类文件:
在AnotherController.php中:
Yii::import('application.controllers.SomeController');
public class AnotherController extends SomeController {
// ...
}
Run Code Online (Sandbox Code Playgroud)
如果你还需要从url访问基类,你可以使用上面的方法.否则,您可以将基类放在protected/components中,就像您已经想到的那样.
Yii自动加载仅在文件名与文件所包含的类具有相同名称时才起作用.含义class SomeController应该在SomeController.php文件中.
进行这些更改,它应该工作.
一个有用的维基:了解自动加载帮助程序类和帮助程序函数.
指南链接:
类文件应以它们包含的公共类命名.