laravel5.6框架中的模版管理

假设我们都把模版放在框架的public目录的templates目录里

那么实现该模版管理功能的思路:

1、在模版目录里得有package.json文件,文件中的内容如下:

{
  "name":"as",
  "thumb":"thumb.jpg"
}

2、创建模版控制器TemplateController,里面有一个显示所有模版的方法:

public function index(TemplateService $templateService)
    {
        $templates = $templateService->getTemplates();
        return view('admin::templates.index', compact('templates'));
    }

3、创建一个TemplateService类,用来获取所有的模版信息

    /**
     * 获取所有的模版
     */
    public function getTemplates()
    {
        //循环模版目录,获取所有的文件夹名
        $data = glob(public_path('/templates/*'));
        $config = [];
        foreach ($data as $item) {
            if ($data = $this->getConfig($item)) {
                $config[] = $data;
            }
        }

        return $config;
    }

    /**
     * 获取每个模版中配置文件的数据
     * @param $template
     * @return bool|mixed|string
     */
    public function getConfig($template)
    {
        $file = $template . '/package.json';
        // 判断模版文件夹中是否有package.json文件,没有的话就不认该模版
        if (is_file($file)) {
            //获取配置文件中的数据
            $data = file_get_contents($file);
            $data = json_decode($data, true);
            //获取缩略图的完整路径
            $data['thumb'] = asset('templates') . '/' . basename($template) . '/' . $data['thumb'];
            return $data;
        }
    }

4、在页面中显示之后,有一个设置应用模版的按钮,点击之后调到模版控制器中的设置应用模版的方法


    /**
     * 设置应用模版
     */
    public function setTemplateDefault($templateName)
    {
        //houdunwang/laravel-module扩展包自带的保存配置信息的方法
        //第一个参数必须是数组,第二个参数是配置文件
        \HDModule::saveconfig(['template'=>$templateName], 'config');
        exit(json_encode(['code'=>1, 'msg'=>'应用模版设置成功']));
    }

5、与保存相对的获取配置文件中的信息方法

\HDModule::config('admin.config.name')
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值