karlomikus / theme
为您的Laravel 5.*项目添加主题支持
v1.0.5
2019-04-27 11:26 UTC
Requires
- php: >=5.5.9
Requires (Dev)
- mockery/mockery: 0.9.4
- orchestra/testbench: 3.0.*
- phpunit/phpunit: ~4.0
README
为您的Laravel 5.*项目添加主题支持。
特性
- 自定义主题位置
- 支持主题继承与回退
- 主题资源加载
- Artisan控制台命令
安装
在终端中像这样要求它:
$ composer require karlomikus/theme
或者将包添加到您的composer文件中
"karlomikus/theme": "1.*"
接下来,在您的config/app.php
文件中添加新的服务提供者和外观
// Service provider Karlomikus\Theme\ThemeServiceProvider::class // Facade 'Theme' => Karlomikus\Theme\Facade\Theme::class
接下来,您需要发布配置文件
$ php artisan vendor:publish
这将在您的配置目录中创建一个theme.php文件,在其中您可以定义默认的主题目录路径。
主题设置
在您的主题目录中创建新的文件夹(默认:public/themes),并添加views文件夹(其中将包含所有自定义视图)和theme.json文件(包含有关主题的信息)。
{ "name": "Theme name", "author": "Karlo Mikuš", "description": "Default theme description", "version": "1.0", "namespace": "theme-folder", "parent": null }
这些都是可用的属性,但必需的属性包括:name
、author
和namespace
。namespace
的值必须是主题文件夹的名称。
如果您想使您的主题依赖于其他主题视图,只需在parent
属性中包含父主题命名空间。
示例文件夹结构
- public/
- themes/
- theme-1/
- views/
- theme.json
用法
库将首先检查主题目录中所有可用的有效主题。
然后您可以设置一个主题,通过它的命名空间
Theme::set('theme-namespace');
然后像在laravel中通常所做的那样调用视图
view('home');
这首先会检查当前主题目录中是否存在home.blade.php。如果没有找到,它将检查父主题,最后回退到默认的laravel视图位置。
您也可以使用ThemeInterface注入主题实例。
use Karlomikus\Theme\Contracts\ThemeInterface; private $theme; public function __construct(ThemeInterface $theme) { $this->theme = $theme }
主题路径
您可以在config/theme.php文件中设置主题文件夹的默认路径。请注意,目前主题文件夹必须位于public文件夹内部。
可用方法
以下是您可以访问的方法列表
// Activate/set theme Theme::set('theme-namespace'); // Get all available themes as an array Theme::all(); // Get currently active Theme::get(); // Get theme by namespace Theme::get('specific-namespace'); // Override default theme path Theme::setDefaultThemePath('new/path/to/themes'); // Check if theme exists Theme::has('theme-namespace'); // Render theme path URL theme_url('assets/style.css');
Artisan命令
获取所有找到的主题的表格
$ php artisan theme:list +------------------+-------------+------------+ | Name | Author | Namespace | +------------------+-------------+------------+ | Bootstrap theme | Karlo Mikus | bootstrap | | Default theme | Test Author | default | | Foundation theme | Lorem Ipsum | foundation | | Test theme | Dolor Sitha | test | +------------------+-------------+------------+
创建包含配置文件的主题目录
$ php artisan theme:make Template name: > Theme name Template author: > Firstn Lastn Theme created succesfully!
变更日志
请参阅变更日志获取更多关于最近更改的信息。
待办事项
- 与我联系以获取想法