1、模板全局配置是怎么加载的
在 HomeBaseController.php 的 fech方法
$more = $this->getThemeFileMore($template);
用
echo ThemeModel::getLastSql(); 输出sql语句 :SELECT `more` FROM `cmf_theme_file` WHERE `theme` = 'w0s' AND ( `is_public` = 1 OR `file` = 'portal/index' ) 获取了声明公共配置和当前模板文件的模板配置。
可见,不管当前theme下那个模板文件配置,只要is_public=1,就可以加载vars 和 widgets 。 全局配置里的变量和控件就是这样加载的。。。。。
getThemeFileMore函数源码:
/*** 获取模板文件变量* @param string $file* @param string $theme* @return array*/private function getThemeFileMore($file, $theme = ""){//TODO 增加缓存$theme = empty($theme) ? cmf_get_current_theme() : $theme;// 调试模式下自动更新模板if (APP_DEBUG) {$themeModel = new ThemeModel();$themeModel->updateTheme($theme);}$themePath = config('cmf_theme_path');$file = str_replace('\\', '/', $file);$file = str_replace('//', '/', $file);$file = str_replace(['.html', '.php', $themePath . $theme . "/"], '', $file);$files = Db::name('theme_file')->field('more')->where(['theme' => $theme])->where(function ($query) use ($file) {$query->where(['is_public' => 1])->whereOr(['file' => $file]);})->select();echo ThemeModel::getLastSql();$vars = [];$widgets = [];foreach ($files as $file) {$oldMore = json_decode($file['more'], true);if (!empty($oldMore['vars'])) {foreach ($oldMore['vars'] as $varName => $var) {$vars[$varName] = $var['value'];}}if (!empty($oldMore['widgets'])) {foreach ($oldMore['widgets'] as $widgetName => $widget) {$widgetVars = [];if (!empty($widget['vars'])) {foreach ($widget['vars'] as $varName => $var) {$widgetVars[$varName] = $var['value'];}}$widget['vars'] = $widgetVars;$widgets[$widgetName] = $widget;}}}return ['vars' => $vars, 'widgets' => $widgets];}
2、新增网站配置项怎么全局使用
目前cmf5的后台配置并没有网站设置里并没有包含:公司名称,地址,电话,手机,在线客服等公司信息,两种方式可以实现这些配置项:
一是在声明了is_public=1的模板里增加模板变量(例如:config.json,head模板,foot模板)。 优点:无需修改源程序,升级不用担心覆盖,缺点:这些信息属于网站信息,应该独立于模板之外,否则换模板的话还需要重新设置。
二是在后台网站设置里增加新的配置项目。无须担心换模板,需要修改控制器基类,升级就会被覆盖,每次升级都需要修改一下。
这里使用第二种方法
app\admin\controller 新增ComSettingController.php
namespace app\admin\controller;use cmf\controller\AdminBaseController; use think\Validate;class ComSettingController extends AdminBaseController {/*** 公司信息配置* @adminMenu(* 'name' => '公司信息配置',* 'parent' => 'admin/Setting/default',* 'display'=> true,* 'hasView'=> true,* 'order' => 10,* 'icon' => '',* 'remark' => '公司信息配置',* 'param' => ''* )*/public function index(){$comSetting = cmf_get_option('com_setting');$this->assign($comSetting);return $this->fetch();}/*** 公司信息配置* @adminMenu(* 'name' => '公司信息配置提交保存',* 'parent' => 'index',* 'display'=> false,* 'hasView'=> false,* 'order' => 10000,* 'icon' => '',* 'remark' => '公司信息配置提交保存',* 'param' => ''* )*/public function indexPost(){$post = array_map('trim', $this->request->param());//这里判断数据有效 cmf_set_option('com_setting', $post);$this->success("保存成功!");}}
后台菜单增加对应菜单
这样就新增了 com_setting配置项
怎么使用???
(1)在对应controller里增加 例如:indexController,只能在当前模板使用
$comSetting = cmf_get_option('com_setting');
$this->assign(‘com_info’.$comSetting);
这样就可以在模板index.html 使用 com_info.company 来显示公司名称
(2)要想全局使用,需要加到controller的基类 HomeBaseController里,在simplewind\cmf\controller目录下
public function _initialize(){// 监听home_inithook('home_init');parent::_initialize();$siteInfo = cmf_get_site_info();View::share('site_info', $siteInfo);$comSetting = cmf_get_option('com_setting');View::share('com_info', $comSetting);}
这样就可以在任何模板里使用 com_info.company