在项目开发过程中会遇到一个网站有不同风格,本文详细介绍
Thinkphp5 实现
模板主题多个模板切换
一、在Config中配置view_path模板路径代码如下
'template' => [
'type' => 'Think',
'view_path' => '/template/',
'view_suffix' => 'html',
'view_depr' => DS,
'tpl_begin' => '{',
'tpl_end' => '}',
'taglib_begin' => '{',
'taglib_end' => '}',
],
二、所有控制器继承 Base控制器,Base 控制器代码如下:
class Base extends Controller
{
public function __construct(Request $request)
{
if($request->isMobile())
{
config('template.view_path','template/default/mobile/'.$request->module()."/");
}
else{
config('template.view_path','template/default/web/'.$request->module()."/");
}
parent::__construct($request);
}
}
PS:必须在构造函数里用config,构造函数过后调用就没用了