一种(显而易见的、简单的)解决方案可能是将控制器放在 common/controllers 中,然后在应用程序控制器文件夹中扩展类,没有任何内容。
所以可能有一个控制器common/controllers/ExampleController
:
namespace common\controllers;
class ExampleController extends yii\web\Controller {
public function actionIndex() {...}
public function actionView() {...}
public function actionCreate() {...}
public function actionUpdate() {...}
public function actionDelete() {...}
...
}
派生类在frontend/controllers/ExampleController
:
namespace frontend\controllers;
class ExampleController extends \common\controllers\ExampleController {
// empty class
}
注意:该controllerNamespace
设置不再需要更改。
另一种类似的方法是在 frontend/controllers 文件夹中有一个符号链接,该链接指向 common/controllers 中的类。但我不确定这是否会出现问题(符号链接可能是版本控制系统中的问题,当它们在运行时得到解决时可能会导致错误......)。
如果您还想在公共部分(common/views/example)中拥有视图,您可以在扩展类中覆盖以getViewPath()
更改将搜索视图文件的位置。
namespace frontend\controllers;
class ExampleController extends \common\controllers\ExampleController {
public function getViewPath() {
return '@common/views/example';
}
}
注意:要更改使用的布局文件,您可以使用Controller::$layout
.