Table of Contents
laravel 5 实现模板主题功能(续),laravel模板
Home php教程 php手册 laravel 5 实现模板主题功能(续),laravel模板

laravel 5 实现模板主题功能(续),laravel模板

Jun 13, 2016 am 09:13 AM
laravel template

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模板主题功能的实现,我们就探讨到这里了,希望大家能够喜欢。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to get the return code when email sending fails in Laravel? How to get the return code when email sending fails in Laravel? Apr 01, 2025 pm 02:45 PM

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...

How to implement the custom table function of clicking to add data in dcat admin? How to implement the custom table function of clicking to add data in dcat admin? Apr 01, 2025 am 07:09 AM

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

Laravel Redis connection sharing: Why does the select method affect other connections? Laravel Redis connection sharing: Why does the select method affect other connections? Apr 01, 2025 am 07:45 AM

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...

Laravel multi-tenant extension stancl/tenancy: How to customize the host address of a tenant database connection? Laravel multi-tenant extension stancl/tenancy: How to customize the host address of a tenant database connection? Apr 01, 2025 am 09:09 AM

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 Eloquent ORM in Bangla partial model search) Laravel Eloquent ORM in Bangla partial model search) Apr 08, 2025 pm 02:06 PM

LaravelEloquent Model Retrieval: Easily obtaining database data EloquentORM provides a concise and easy-to-understand way to operate the database. This article will introduce various Eloquent model search techniques in detail to help you obtain data from the database efficiently. 1. Get all records. Use the all() method to get all records in the database table: useApp\Models\Post;$posts=Post::all(); This will return a collection. You can access data using foreach loop or other collection methods: foreach($postsas$post){echo$post->

How to effectively check the validity of Redis connections in Laravel6 project? How to effectively check the validity of Redis connections in Laravel6 project? Apr 01, 2025 pm 02:00 PM

How to check the validity of Redis connections in Laravel6 projects is a common problem, especially when projects rely on Redis for business processing. The following is...

Laravel database migration encounters duplicate class definition: How to resolve duplicate generation of migration files and class name conflicts? Laravel database migration encounters duplicate class definition: How to resolve duplicate generation of migration files and class name conflicts? Apr 01, 2025 pm 12:21 PM

A problem of duplicate class definition during Laravel database migration occurs. When using the Laravel framework for database migration, developers may encounter "classes have been used...

Laravel Introduction Example Laravel Introduction Example Apr 18, 2025 pm 12:45 PM

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

See all articles