yii2如何覆盖每个模块的配置
我正在尝试构建一个使用前端框架的yii2应用程序(在我的例子中,如果有必要的话,就是riotjs)。因此,yii2应用程序basic和yii2应用程序advanced并不真正符合我的需要,因此我以以下方式创建了自己的结构:yii2如何覆盖每个模块的配置,yii2,Yii2,我正在尝试构建一个使用前端框架的yii2应用程序(在我的例子中,如果有必要的话,就是riotjs)。因此,yii2应用程序basic和yii2应用程序advanced并不真正符合我的需要,因此我以以下方式创建了自己的结构: /controllers /IndexController.php /modules /v1 /Module.php /users /Module.php /controllers
/controllers
/IndexController.php
/modules
/v1
/Module.php
/users
/Module.php
/controllers
...
/themes
/v1
/layouts
/main.php
/index
/index.php
/partials
/navpanel.js
/assets
(bower assets here because it is still in the dev phase)
/config
/main.php
/main-local.php
/public
/index.php
/assets
与此问题相关的配置:
'defaultRoute' => 'index/index',
'components' => [
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'_api/<module:\w+>/<controller:\w+>/<action:\w+>' => '<module>/<controller>/<action>'
]
],
'errorHandler' => [
'errorAction' => 'index/index'
],
'view' => [
'theme' => [
'basePath' => '@app/themes/v1',
'baseUrl' => '@app/themes/v1',
'pathMap' => ['@app/views' => '@app/themes/v1']
]
]
],
'modules' => [
'v1' => [
'class' => 'app\modules\v1\Module'
]
]
“defaultRoute”=>“索引/索引”,
“组件”=>[
“urlManager”=>[
“enablePrettyUrl”=>true,
'showScriptName'=>false,
“规则”=>[
“_api//”=>“/”
]
],
'errorHandler'=>[
“errorAction”=>“索引/索引”
],
“查看”=>[
“主题”=>[
'basePath'=>'@app/themes/v1',
'baseUrl'=>'@app/themes/v1',
“路径图”=>['@app/views'=>'@app/themes/v1']
]
]
],
“模块”=>[
“v1”=>[
'class'=>'app\modules\v1\Module'
]
]
因此,所有后端逻辑都存储在子模块中,子模块由其父模块进行版本控制。这使我能够模块化地为api创建新版本。主题也是如此。url管理器中的规则是在所有模块前面加上\u api/
。默认路由和错误处理程序将分派到相同的操作,因为riotjs将处理错误(例如,如果有人打开浏览器并转到不存在的路由,则首先呈现riotjs模板,然后riotjs将显示“页面不存在”的对话框)
这就是我被困的地方。我想为父模块和子模块提供不同的配置。我希望主模块有一个不同的、基于JSON的错误处理程序。其次,我希望将所有URL规则保留在子模块中。因此,模块用户的REST规则将驻留在模块中,而不是主配置文件中。如果我将这些规则存储在主配置中,设计将失去预期的模块性
如何覆盖每个模块的这些配置?在module.php中
public function init()
{
parent::init();
Yii::$app->setComponents(require(__DIR__ . '/config.php'));
}
和config.php示例:
return [
'response' => [
'format'=>'json',
'class' => 'yii\web\Response',
],
];
因此,对于
v1
模块,您需要一组配置,user
模块需要一个完全不同的配置?用户需要v1的配置以及一些其他配置。例如,如果我将错误处理程序添加到模块v1,我希望错误处理程序也能在用户界面中工作