akbardwi / laratheme
为Laravel提供的主题和资产管理
Requires
- hassankhan/config: ^2.1
- symfony/yaml: ^5.2 || ^6.0 || ^7.0
Requires (Dev)
- graham-campbell/testbench: ^3.3
- phpunit/phpunit: ^5.0
Suggests
- symfony/yaml: Allows for parsing of YAML theme info and change log files
README
Laratheme是一个Laravel主题和资产管理包。您可以将此包轻松集成到任何基于Laravel的项目中。
特性
- 自定义主题路径
- 覆盖主题
- 支持父主题
- 无限查找父视图
- 资产查找
- 主题翻译支持
- 多主题配置扩展
- 多主题变更日志扩展
- Artisan控制台命令
- 通过中间件仅通过特定路由启用主题
- 几乎一切都可以自定义
- 还支持Laravel 5.5到Laravel 10
安装
Laratheme是Laravel包,您可以通过Composer安装它。在您的项目目录中运行以下命令:
composer require akbardwi/laratheme
等待一段时间,Composer将自动在您的项目中安装Laratheme。
配置
在Laravel 5.5以下版本中,您必须在config/app.php
配置文件中调用此包服务。为此,请在providers
数组中添加以下行:
Akbardwi\Laratheme\Providers\LarathemeServiceProvider::class,
在Laravel 5.5以下版本中,要使用外观,您必须在app.php
中的aliases
数组中添加此行:
'Theme' => Akbardwi\Laratheme\Facades\Theme::class,
现在在您的终端中运行此命令以发布此包资源:
php artisan vendor:publish --provider="Akbardwi\Laratheme\Providers\LarathemeServiceProvider"
Artisan命令
从您的项目目录中运行以下命令:
创建主题目录
php artisan theme:create your_theme_name What is theme title?: > What is theme description? []: > What is theme author name? []: > What is theme version? []: > Any parent theme? (yes/no) [no]: > y What is parent theme name?: >
所有主题列表
php artisan theme:list +----------+---------------------+---------+----------+ | Name | Author | Version | Parent | +----------+---------------------+---------+----------+ | themeone | Akbar Dwi Syahputra | 1.1.0 | | | themetwo | Akbar Dwi Syahputra | 1.0.0 | themeone | +----------+---------------------+---------+----------+
示例文件夹结构
- app/
- ..
- ..
- themes/
- themeone/
- assets
- css
- app.css
- img
- js
- lang
- en
-content.php
- views/
- layouts
- master.blade.php
- welcome.blade.php
- changelog.yml
- theme.json
- themetwo/
您可以从config/theme.php
中更改theme.json
和changelog.yml
的名称
// .. 'config' => [ 'name' => 'theme.json', 'changelog' => 'changelog.yml' ], // ..
支持json
、yml
、yaml
、php
、ini
、xml
扩展。
例如
// .. 'config' => [ 'name' => 'theme.json', 'changelog' => 'changelog.json' ], // ..
然后运行上述的theme:create
命令。
现在请查看API列表文档。
视图查找流程
假设您想要找到welcome.blade.php
- At first check your active theme
- If `welcome.blade.php not found in active theme then search parent recursively
- If `welcome.blade.php not found in parents theme then search laravel default view folder resources/views
API列表
set
要切换当前主题,您可以使用set
方法。
Theme::set('theme-name');
get
要获取当前主题详细信息,您可以使用get
方法
Theme::get(); // return Array
您也可以获取特定主题的详细信息
Theme::get('theme-name'); // return Array
Theme::get('theme-name', true); // return Collection
current
检索当前主题的名称
Theme::current(); // return string
all
检索所有主题信息
Theme::all(); // return Array
has
要获取主题是否存在,您可以使用has
方法
Theme::has(); // return bool
getThemeInfo
要获取指定主题的信息
$themeInfo = Theme::getThemeInfo('theme-name'); // return Collection $themeName = $themeInfo->get('name'); // or $themeName = $themeInfo['name'];
还支持回退
$themeInfo = Theme::getThemeInfo('theme-name'); // return Collection $themeName = $themeInfo->get('changelog.versions'); // or $themeName = $themeInfo['changelog.versions']; // or you can also call like as multi dimension $themeName = $themeInfo['changelog']['versions'];
assets
要绑定主题资产,您可以使用assets
方法
Theme::assets('your_asset_path'); // return string
它生成在BASE_URL/theme_roots/your_active_theme_name/assets/your_asset_path
如果your_asset_path
不存在,则激活主题的父级资产文件夹也可以。看起来像BASE_URL/theme_roots/your_active_theme_parent_name/assets/your_asset_path
当使用辅助函数时,您也可以获取资产路径
themes('your_asset_path'); // return string
如果您想要绑定特定主题的资产
Theme::assets('your_theme_name:your_asset_path'); // return string // or themes('your_theme_name:your_asset_path'); // return string
假设您想要在blade中绑定app.css
。以下代码适用:
<link rel="stylesheet" href="{{ themes('app.css') }}">
特定主题资产
<link rel="stylesheet" href="{{ themes('your_theme_name:app.css') }}">
lang
lang
方法使用您的当前主题的本地化文件翻译给定的语言行本地化文件
echo Theme::lang('content.title'); // return string // or echo lang('content.title'); // return string
也支持
echo Theme::lang('content.title', [your replace array], 'your desire locale'); // return string // or echo lang('content.title', [your replace array], 'your desire locale'); // return string
如果您想要绑定特定主题的资产
echo Theme::lang('your_theme_name::your_asset_path'); // return string // or echo lang('your_theme_name::your_asset_path'); // return string
如何在路由中使用
Route::get('/', function () { Theme::set('your_theme_name'); return view('welcome'); });
这将首先检查当前主题目录中是否有welcome.blade.php。如果没有找到,则检查父主题,最后回退到默认的Laravel视图位置。
如果您想要指定主题视图
Route::get('/', function () { Theme::set('your_theme_name'); return view('your_theme_name::welcome'); });
使用路由中间件设置主题
如果想要为每个路由定义一个主题,已经包含了一个辅助中间件。要使用它
首先在app\Http\Kernel.php
中注册它
protected $routeMiddleware = [ // ... 'theme' => \Akbardwi\Laratheme\Middleware\RouteMiddleware::class, ];
现在您可以将中间件应用于路由或路由组。例如:
Route::group(['prefix' => 'admin', 'middleware'=>'theme:Your_theme_name'], function() { // ... Add your routes here // The Your_theme_name will be applied. });
使用Web中间件设置主题。
如果想要为每个路由定义一个主题,已经包含了一个辅助中间件。要使用它
首先在app\Http\Kernel.php
中注册它
protected $middlewareGroups = [ 'web' => [ // ... \Akbardwi\Laratheme\Middleware\WebMiddleware::class, ], // ... ];
主题设置来自 config/theme.php
文件。
然后,在您的控制器中,您可以像平常一样调用视图。
return view('home'); // This will load the home.blade.php from the the folder you set in your `config/theme.php`
依赖注入
您还可以使用ThemeContract注入主题实例,例如:
use Akbardwi\Laratheme\Contracts\ThemeContract; private $theme; public function __construct(ThemeContract $theme) { $this->theme = $theme }
故障排除
在运行 vendor publish
命令后清除配置(请参阅配置部分)以保存与配置缓存相关的问题,运行:
php artisan config:cache
php artisan config:clear