Yii2 之 frontend 子模块实践之二:构建子模块的独立配置

▪ 环境

基于《Yii2 之 frontend 子模块实践之一:添加前后台子模块》。

▪ 前言

默认情况下,两个 子模块 直接使用 应用主体 的配置:/frontend/config/main.php,这其实是一个非常大的坑:

  • 当你想前台启用 URL 美化的时候,你修改了 /frontend/config/main.php 并启用成功,但是此时你会发现后台居然也跟着启用了URL美化,这并不是你所想要的
  • 当你在 /frontend/config/main.php 设置了后台的管理员身份验证类 'identityClass' => 'common\models\AdminIdentity' 后,你会发现前台的会员身份验证类 UserIdentity 没有地方可以设置了

还有很多组件,如果全部统一使用 /frontend/config/main.php 配置文件,将会出现更多的问题,所以比较好解决办法就是:应用主体 的配置 + 子模块的独立配置

▪ 配置子模块的别名

编辑 indexadmin 子模块 根目录下的 Module.php 文件,在 init() 函数里添加模块的物理路径别名:

// 设置别名
Yii::setAlias('@module', '@app/modules/'.$this->id);

根据项目的实际需要你可以在此处添加其他别名,这里设置的 @module 将在下面的内容使用

▪ 加载子模块的配置

编辑 indexadmin 子模块 根目录下的 Module.php 文件,在 init() 函数里添加子模块配置的加载代码:

// 设置别名
Yii::setAlias('@module', '@app/modules/'.$this->id);

// 初始模块配置
$config = require(__DIR__.'/Config.php');
$components = Yii::$app->getComponents();

foreach( $config['components'] AS $k=>$component ){
    if( isset($component['class']) && isset($components[$k]) == false ) continue;
    $config['components'][$k] = array_merge($components[$k], $component);
}

Yii::configure(Yii::$app, $config);

代码解释:

// 加载子模块独立配置文件
// 请在每个子模块的 Module.php 同级目录下创建 Config.php 文件
$config = require(__DIR__.'/Config.php');

// 获取应用程序的组件
$components = Yii::$app->getComponents();

// 遍历子模块独立配置的组件部分,并继承应用程序的组件配置
foreach( $config['components'] AS $k=>$component ){
    if( isset($component['class']) && isset($components[$k]) == false ) continue;
    $config['components'][$k] = array_merge($components[$k], $component);
}

// 将新的配置设置到应用程序
// 网上很多文章搜过来都是写 Yii::configure($this, $config),但是并不适用子模块,必须写 Yii::$app
Yii::configure(Yii::$app, $config);

▪ 编写子模块的配置文件

编辑 index 和 admin 子模块 根目录下的 Config.php 文件,这里是我常用的配置,具体可自行按实际需要配置。

前台配置示例:
<?php
$configs['layout']                                              = '@module/views/layouts/main';
$configs['components']                                          = array();

$configs['components']['i18n']['translations']                  = array();
$configs['components']['i18n']['translations']['*']             = array();
$configs['components']['i18n']['translations']['*']['class']    = 'yii\i18n\PhpMessageSource';
$configs['components']['i18n']['translations']['*']['basePath'] = '@module/languages';

// $configs['components']['user']                               = array();
// $configs['components']['user']['identityClass']              = 'common\models\MemberIdentity';
// $configs['components']['user']['identityCookie']             = array('name'=>'_identity-frontend-index', 'httpOnly'=>true);
// $configs['components']['user']['enableAutoLogin']            = true;

$configs['components']['session']                               = array('name'=>'advanced-frontend-index');
$configs['components']['request']                               = array('enableCsrfValidation'=>false, 'csrfParam'=>'_csrf-frontend-index');

return $configs;
后台配置示例:
<?php
$configs['layout']                                              = '@module/views/layouts/main';
$configs['components']                                          = array();

$configs['components']['i18n']['translations']                  = array();
$configs['components']['i18n']['translations']['*']             = array();
$configs['components']['i18n']['translations']['*']['class']    = 'yii\i18n\PhpMessageSource';
$configs['components']['i18n']['translations']['*']['basePath'] = '@module/languages';

$configs['components']['user']                                  = array();
$configs['components']['user']['identityClass']                 = 'common\models\AdminIdentity';
$configs['components']['user']['identityCookie']                = array('name'=>'_identity-frontend-admin', 'httpOnly'=>true);
$configs['components']['user']['enableAutoLogin']               = true;

$configs['components']['session']                               = array('name'=>'advanced-frontend-admin');
$configs['components']['request']                               = array('enableCsrfValidation'=>false, 'csrfParam'=>'_csrf-frontend-kernel');

return $configs;
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值