shipu/themevel

laravel 的主题和资产管理

资助包维护!
shipu

安装量: 27,013

依赖: 1

建议者: 0

安全: 0

星标: 345

关注者: 11

分支: 69

开放问题: 4

v3.0.4 2023-08-07 20:45 UTC

This package is auto-updated.

Last update: 2024-09-07 23:19:13 UTC


README

Latest Stable Version Latest Stable Version Latest Unstable Version License

Themevel 是一个 Laravel 主题和资产管理包。您可以轻松地将此包集成到任何基于 Laravel 的项目中。

功能

  • 自定义主题路径
  • 覆盖主题
  • 支持父主题
  • 无限父视图查找
  • 资产查找
  • 支持主题翻译器
  • 多主题配置扩展
  • 多主题变更日志扩展
  • Artisan 控制台命令
  • 通过中间件仅启用特定路由的主题
  • 几乎一切都可以自定义
  • 也支持 Laravel 5.5 到 Laravel 10

安装

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.jsonchangelog.yml 的名称

// ..
'config' => [
    'name' => 'theme.json',
    'changelog' => 'changelog.yml'
],
// ..

支持 jsonymlyamlphpinixml 扩展名。

例如

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

要获取主题是否存在

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' => \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中间件设置主题

如果您想为每个路由定义一个主题,则默认包含了一个辅助中间件。要使用它

首先在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

致谢

对本项目的支持

嘿,兄弟!帮帮我,给我几杯🍻!

Beerpay Beerpay