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_init hook('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