enver / themevel
为laravel 5提供主题和资源管理
Requires
- hassankhan/config: ^3.0
- symfony/yaml: ^5.2
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
Themevel是一个Laravel主题和资源管理包。您可以轻松地将此包集成到任何基于Laravel的项目中。
功能
- 自定义主题路径
- 覆盖主题
- 支持父主题
- 无限父视图查找
- 资源查找
- 主题翻译支持
- 多个主题配置扩展
- 多个主题变更日志扩展
- Artisan控制台命令
- 通过中间件仅通过特定路由启用主题
- 几乎一切都可以自定义
- 也支持Laravel 7.0+、8.0+、9.0+
安装
Themevel是一个Laravel包,因此您可以通过Composer安装它。在您的项目目录中从终端运行此命令
composer require shipu/themevel
稍等片刻,Composer将自动在您的项目中安装Themevel。
配置
在Laravel 5.5以下版本中,您必须在config/app.php配置文件中调用此包服务。为此,在app.php中的providers数组中添加此行
Shipu\Themevel\Providers\ThemevelServiceProvider::class,
在Laravel 5.5以下版本中,要使用外观,您必须在app.php中的aliases数组中添加此行
'Theme' => Shipu\Themevel\Facades\Theme::class,
现在,在您的终端中运行此命令以发布此包资源
php artisan vendor:publish --provider="Shipu\Themevel\Providers\ThemevelServiceProvider"
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 | Shipu Ahamed | 1.1.0 | | | themetwo | Shipu Ahamed | 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方法。
Theme::set('theme-name');
获取
要获取当前主题详细信息,您可以使用get方法
Theme::get(); // return Array
您还可以获取特定主题的详细信息
Theme::get('theme-name'); // return Array
Theme::get('theme-name', true); // return Collection
当前
检索当前主题的名称
Theme::current(); // return string
全部
检索所有主题信息
Theme::all(); // return Array
有
要获取主题是否存在
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'); });
使用路由中间件设置主题
如果您想为每个路由定义一个主题,Laravel提供了一个内置的辅助中间件。要使用它
首先在app\Http\Kernel.php中注册它
protected $routeMiddleware = [ // ... 'theme' => \Shipu\Themevel\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中间件设置主题
如果您想为每个路由定义一个主题,Laravel提供了一个内置的辅助中间件。要使用它
首先在app\Http\Kernel.php中注册它
protected $middlewareGroups = [ 'web' => [ // ... \Shipu\Themevel\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 Shipu\Themevel\Contracts\ThemeContract; private $theme; public function __construct(ThemeContract $theme) { $this->theme = $theme }
故障排除
运行vendor publish后清除配置(见配置部分)以保存与配置缓存相关的问题,运行
php artisan config:cache
php artisan config:clear
致谢
对此项目的支持
嘿兄弟!帮帮我,给我点🍻吧!