
效果说明:
使用行为的方式做主题自动切换,可以不需要继承基类,不需要在控制器里来修改配置,完全无侵入。
核心代码:
/*根据配置和来访设备类型自动切换为电脑主题或手机主题。 S*/
$default_view_path = Config::get('template.view_path');//获取用户访问的模块所设置的模版目录
if ($default_view_path != '') {
$request = Request::instance();
$theme_path = Config::get('theme.pc'); //设置主题默认路径为电脑端
if ($request->isMobile()){
$theme_path = Config::get('theme.mobile'); //设置主题默认路径为电脑端
}
$module_path = $request->module();//获取模块名称
Config::set('template.view_path', $default_view_path . $theme_path . '/' . $module_path . '/');//根据模块名称和来访设备类型重置模版路径设置
}
/*根据配置和来访设备类型自动切换为电脑主题或手机主题。 E*/
原理:在模块初始化之前根据设备类型和设置的主题目录设置模块加载目录。主要代码目录:
tp5setTheme\application\behavior\SetTheme.php

tp5setTheme\application\tags.php

tp5setTheme\application\config.php

tp5setTheme\application\index\config.php

最后:雕虫小技,大神勿笑,如果有更好的方法欢迎分享。
完整代码在附件
tp5setTheme.zip
( 370.3 KB 下载:142 次 )
最佳答案
