roland/laravel-theme-manager

Laravel 5 主题:每个主题的资产和视图文件夹。主题继承。Blade 集成等...

1.1 2018-07-16 15:23 UTC

This package is not auto-updated.

Last update: 2024-09-29 05:55:19 UTC


README

本包是 Laravel 的主题管理包,提供一系列工具,帮助您轻松快速地为您基于 Laravel 的项目构建主题管理系统。本包的目标是尽可能地保持灵活性,并提供一个内部使用的方法。

支持

本包支持 BladePHPTwig 模板引擎。

功能

本包提供以下工具

  • 在运行时更改主题。
  • 获取主题元信息。
  • 支持回退模板。
  • 使用 URL 查询参数预览主题。
  • 扩展主题功能。

##安装

要开始使用本包,请将以下行添加到您的 composer.json 文件中,并运行 composer update

"require": {
	"roland/laravel-theme-manager": "~1.0"
}

或者,从您的终端运行 composer require 通过 Composer 包管理器安装包

composer require roland/laravel-theme-manager

服务提供者 & Facade

如果您禁用了 Laravel 的自动包发现功能,这将有所帮助。

// Service Provider
Roland\Theme\ThemeServiceProvider::class,

// Facade
'Theme' => Roland\Theme\Facades\Theme::class,

发布配置

大部分包都是预配置的,这样您就可以在安装后立即开始构建您的 API。您可以使用 .env 文件或 config/themes.php 来配置大部分包。

您也可以使用以下 Artisan 命令发布配置文件

php artisan vendor:publish --provider="Roland\Theme\ThemeServiceProvider"

启用或禁用包

您可以将值设置为 true 或 false 以启用或禁用包。

enable => true,

默认主题提供者

在此处您可以指定以下哪个主题提供者连接要作为您的默认提供者。

可用驱动程序:"file"

'driver' => 'file',

默认主题

在此处您可以指定默认主题。

'theme' => 'default',

主题路径

在此处您可以指定主题的路径。

'path' => base_path('resources/themes'),

##基本用法

创建主题

要创建主题,首先,将 themes 文件夹添加到您的应用 resources 文件夹中。

themes 文件夹中,您创建的任何带有 theme.json 文件的文件夹都代表一个主题。

文件夹结构如下

resources
└── themes
    ├── [Theme folder]
    |   └── theme.json
    |   └── welcome.blade.php
    |
    └── [Another theme folder]
        └── theme.json
        └── welcome.blade.php

主题信息文件(theme.json)

只有当存在名为 theme.json 的文件时,本包才识别一个文件夹为主题。

这是主题信息文件的基本结构。您可以在文件中添加任意数量的键值对,并在以后检索。

{
	"theme_title": "Default theme",
	"theme_description": "Default theme for the theme package",
	"theme_uri": "https://domain.com",
	"theme_version": "1.0.0",
	"theme_license": "The MIT License (MIT)",
	"theme_tags": "default, simple",
	"theme_type": "web",
	"author_name": "Jon Deep",
	"author_email": "JonDeep@domain.com",
	"author_uri": "https://domain.com"
}

基本方法

简单地使用当前主题显示视图(欢迎)。否则回退到(索引)。

return Theme::view(['welcome', 'index']);

在运行时设置当前主题。

return Theme::use('mytheme')->view(['welcome']);

设置当前主题并更新默认配置主题。

Theme::set('mytheme');

theme 参数添加到运行时预览主题。

https:///?theme=mytheme

您还可以在运行时更改主题提供者。

return Theme::driver('file')->view(['welcome']);

检查主题是否存在。

Theme::exists('mytheme');

检查所有主题。

Theme::all();

Ps:它将返回一个包含所有已安装主题的 json。

json 形式返回主题信息。

// Return default theme's info
return Theme::info();

// Return provided theme's info
return Theme::info('mytheme');

向视图传递数据。

// with() method
return Theme::with(['name' => 'Victoria'])->view(['welcome']);

// Alternative way
return Theme::view(['welcome'], ['name' => 'Victoria']);

##高级功能

	Theme::extend('riak', function($app)
	{
		return 'Riak theme driver';
	});

	// Chnage the theme driver from route
	return Theme::driver('riak');

配置中间件

  • 发布包: php artisan vendor:publish
  • Http/kernel.php 中添加到受保护的 'theme' => \App\Http\Middleware\CheckTheme::class,
  • 在路由上使用中间件

路由示例:Route::get('/', ['uses' => 'TestController@home'])->middleware('theme:ThemeName');

ps. 使用主题将允许您使用正确的Laravel view(),当前主题:)

许可证

此软件包受MIT许可证(MIT)的许可。