laravel 5 实现模板主题功能(续),laravel模板
laravel 5 实现模板主题功能(续),laravel模板
在之前一篇文章中我介绍了通过定义Response宏的方式来实现动态改变模板文件路径以实现主题功能: laravel实现模板主题功能,但后来我发现这种方法有个弊端,在模板中使用@extends必须显式指定模板路径,这可能造成混乱,我决定还是改变思想,主题和主题之间应该是完全隔离的,不存在就是不存在,不要自动去另外的主题中寻找替代的模板。
而原来定义response宏的方式可以实现,但我决定使用更加规范的方法。
laravel的View类里有一个方法 View::addNamespace ,这个方法在手册"开发扩展包"一节中有提到,不得不说Laravel手册排版逻辑混乱,这个方法说明应当放在"视图"章节才是,题外话就不说了,先来说说这个方法吧。
laravel渲染视图有一种写法:
复制代码 代码如下:
View::make('namespace::path');
//例如 View::make('default::index.index');
如何定义namespace呢,就是通过这个方法啦:
复制代码 代码如下:
View::addNamespace('default',app_path().'/views/default');
聪明的朋友可能已经感觉到了,这个功能可以助我们实现模板主题化,比如:
复制代码 代码如下:
//注册蓝色主题
View::addNamespace('blue',app_path().'/views/blue');
//注册红色主题
View::addNamespace('red',app_path().'/views/red');
//注册绿色主题
View::addNamespace('green',app_path().'/views/green');
之后调用:
复制代码 代码如下:
//渲染绿色主题下的index.index模板
View::make('green::index.index');
然而我们需要事先通过View::addNamespace方法先注册这几个主题的路径映射,并且在渲染的时候需要显式指定namespace.
我感觉不是很方便,难道View不能设定一个默认的namespace吗?这样我们只要一次设置比如:
复制代码 代码如下:
//我们可以把这个写在 __construct 里面
View::setDefaultNamespace('blue',app_path().'/views/blue');
之后:
复制代码 代码如下:
//实际上相当于 View::make('blue::index.index');
View::make('index.index');
更进一步,我们可以通过后台设置主题,把主题名写进数据库,前台读取并设置主题:
复制代码 代码如下:
//假设从数据库中读取配置,Option是模型类
$theme = Option::getByKey('theme');
View::setDefaultNamespace($theme,app_path().'/views/'.$theme);
这样就实现了后台切换主题了。
但是很遗憾,View并没有setDefaultNamespace方法,所以我决定创建一个项目,专门针对laravel进行核心类库扩展,这个功能已经实现,可以查看我的项目:项目地址 ,在src/Keepeye/Laravel/View/查看使用方法吧。
好了,关于laravel模板主题功能的实现,我们就探讨到这里了,希望大家能够喜欢。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Method for obtaining the return code when Laravel email sending fails. When using Laravel to develop applications, you often encounter situations where you need to send verification codes. And in reality...

Laravel schedule task run unresponsive troubleshooting When using Laravel's schedule task scheduling, many developers will encounter this problem: schedule:run...

The method of handling Laravel's email failure to send verification code is to use Laravel...

How to implement the table function of custom click to add data in dcatadmin (laravel-admin) When using dcat...

Laravel - Dump Server - Laravel dump server comes with the version of Laravel 5.7. The previous versions do not include any dump server. Dump server will be a development dependency in laravel/laravel composer file.

The impact of sharing of Redis connections in Laravel framework and select methods When using Laravel framework and Redis, developers may encounter a problem: through configuration...

Custom tenant database connection in Laravel multi-tenant extension package stancl/tenancy When building multi-tenant applications using Laravel multi-tenant extension package stancl/tenancy,...

Laravel - Action URL - Laravel 5.7 introduces a new feature called “callable action URL”. This feature is similar to the one in Laravel 5.6 which accepts string in action method. The main purpose of the new syntax introduced Laravel 5.7 is to directl
