mcpField
S
shop-theme-development

shop-theme-development

github.com/bagisto设计与 UI13267 次安装2 周前更新
代码仓库

关于 shop-theme-development

shop-theme-development 是面向 Bagisto 开发者的主题开发 Skill,指导将店面主题构建为可独立分发和维护的 Laravel 包。内容涵盖视图、资源、服务提供者、主题配置与 Vite 资源构建。

主要功能

  1. 01创建独立的 Laravel 主题包
  2. 02组织主题视图与静态资源
  3. 03配置服务提供者和主题注册
  4. 04使用 Vite 构建主题资源
  5. 05通过软链接或发布流程进行开发

适用场景

  1. 01为 Bagisto 店面创建自定义主题
  2. 02覆盖默认模板并定制页面布局
  3. 03配置主题资源的开发与生产构建
  4. 04打包并发布可维护的主题

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/bagisto/agent-skills --skill shop-theme-development

安装后在 Claude Code 中输入/shop-theme-development确认已加载。

详细介绍

商城主题开发

概述

在 Bagisto 中,商城主题开发涉及创建打包为 Laravel 软件包的自定义店面主题。最终结果是一个独立完整的软件包,可单独分发和维护。

适用场景

在以下情况下启用此技能:

  • 将自定义店面主题创建为软件包
  • 构建用于分发的主题软件包
  • 使用 Vite 驱动的资源
  • 自定义面向客户的页面
  • 覆盖默认商城模板

Bagisto 商城主题架构

核心组件
组件用途位置
主题配置定义可用主题config/themes.php
视图路径Blade 模板文件在主题配置中定义
资源路径CSS、JS、图片在主题配置中定义
主题中间件解析当前主题packages/Webkul/Shop/src/Http/Middleware/Theme.php
主题 Facade管理主题操作packages/Webkul/Theme/src/Themes.php
关键配置属性
// config/themes.php
'shop-default' => 'default',

'shop' => [
    'default' => [
        'name' => 'Default',
        'assets_path' => 'public/themes/shop/default',
        'views_path' => 'resources/themes/default/views',
        'vite' => [
            'hot_file' => 'shop-default-vite.hot',
            'build_directory' => 'themes/shop/default/build',
            'package_assets_directory' => 'src/Resources/assets',
        ],
    ],
],

创建商城主题软件包

目标:构建独立完整的软件包

最终结果应是一个完整的软件包,包含:

  • 软件包中的所有视图
  • 软件包中的所有资源
  • 用于发布的服务提供者
  • 用于编译资源的 Vite 配置
步骤 1:创建软件包结构
mkdir -p packages/Webkul/CustomTheme/src/{Providers,Resources/views}
步骤 2:复制完整的商城资源

将所有商城资源复制到软件包中,以便完全掌控:

# Copy complete shop assets folder
cp -r packages/Webkul/Shop/src/Resources/assets/* packages/Webkul/CustomTheme/src/Resources/assets/

# Copy complete shop views folder
cp -r packages/Webkul/Shop/src/Resources/views/* packages/Webkul/CustomTheme/src/Resources/views/

# Copy shop package.json for dependencies
cp packages/Webkul/Shop/package.json packages/Webkul/CustomTheme/

这样你就能获得:

  • 基于 Tailwind 的完整 CSS 基础
  • 所有 JavaScript 功能
  • 商城组件和布局
  • 图片、字体和静态资源
  • 完整的 Blade 模板结构
  • 用于编译资源的依赖项
步骤 3:添加自定义主页(样板)

复制完成后,创建一个自定义主页,以展示这是一个新主题:

文件: packages/Webkul/CustomTheme/src/Resources/views/home/index.blade.php

<x-shop::layouts>
    <x-slot:title>
        Custom Theme Home
    </x-slot>

    {{-- Hero Section --}}
    <div class="hero-section bg-gradient-to-r from-blue-600 to-purple-600 text-white py-20">
        <div class="container mx-auto px-4 text-center">
            <h1 class="text-5xl font-bold mb-6">
                Welcome to Our Store
            </h1>
            
            <p class="text-xl mb-8 opacity-90">
                Professional theme with modern design
            </p>
            
            <a href="{{ route('shop.search.index') }}" 
               class="bg-white text-blue-600 font-bold py-3 px-8 rounded-lg hover:bg-gray-100 transition duration-300">
                Start Shopping
            </a>
        </div>
    </div>

    {{-- Featured Products --}}
    <div class="container mx-auto px-4 py-16">
        <h2 class="text-3xl font-bold text-center mb-12">
            Featured Products
        </h2>
        
        
    </div>
</x-shop::layouts>
步骤 4:创建自定义布局(可选)

创建自己的主布局,以便完全掌控:

文件: packages/Webkul/CustomTheme/src/Resources/views/layouts/master.blade.php

<!DOCTYPE html>
<html lang="{{ app()->getLocale() }}">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>{{ $title ?? config('app.name') }}</title>
    
    {{-- Load theme assets manually --}}
    @bagistoVite([
        'src/Resources/assets/css/app.css',
        'src/Resources/assets/js/app.js'
    ])
    
    @stack('meta')
    @stack('styles')
</head>
<body class="{{ $bodyClass ?? '' }}">
    @if($hasHeader ?? true)
        @include('custom-theme::layouts.header')
    @endif
    
    <main class="main-content">
        {{ $slot }}
    </main>
    
    @if($hasFooter ?? true)
        @include('custom-theme::layouts.footer')
    @endif
    
    @stack('scripts')
</body>
</html>
步骤 5:创建服务提供者

文件: packages/Webkul/CustomTheme/src/Providers/CustomThemeServiceProvider.php

<?php

namespace Webkul\CustomTheme\Providers;

use Illuminate\Support\ServiceProvider;

class CustomThemeServiceProvider extends ServiceProvider
{
    /**
     * Register services.
     */
    public function register(): void
    {
        //
    }

    /**
     * Bootstrap services.
     */
    public function boot(): void
    {
        // Publish views to resources/themes/custom-theme/views
        $this->publishes([
            __DIR__ . '/../Resources/views' => resource_path('themes/custom-theme/views'),
        ], 'custom-theme-views');
    }
}
步骤 6:配置自动加载

更新 composer.json:

"autoload": {
    "psr-4": {
        "Webkul\\CustomTheme\\": "packages/Webkul/CustomTheme/src"
    }
}

运行:composer dump-autoload

步骤 7:注册服务提供者

添加到 bootstrap/providers.php:

Webkul\CustomTheme\Providers\CustomThemeServiceProvider::class,
步骤 8:更新主题配置

文件: config/themes.php

'shop-default' => 'custom-theme',

'shop' => [
    'default' => [
        'name' => 'Default',
        'assets_path' => 'public/themes/shop/default',
        'views_path' => 'resources/themes/default/views',
        'vite' => [
            'hot_file' => 'shop-default-vite.hot',
            'build_directory' => 'themes/shop/default/build',
            'package_assets_directory' => 'src/Resources/assets',
        ],
    ],
    'custom-theme' => [
        'name' => 'Custom Theme Package',
        'assets_path' => 'public/themes/shop/custom-theme',
        'views_path' => 'resources/themes/custom-theme/views',
        'vite' => [
            'hot_file' => 'custom-theme-vite.hot',
            'build_directory' => 'themes/custom-theme/build',
            'package_assets_directory' => 'src/Resources/assets',
        ],
    ],
],
步骤 9:发布视图
php artisan vendor:publish --provider="Webkul\CustomTheme\Providers\CustomThemeServiceProvider"
步骤 10:清除缓存
php artisan optimize:clear
步骤 11:启用主题
  1. 登录管理面板
  2. 前往设置 → 渠道
  3. 编辑渠道并选择你的主题
  4. 保存
步骤 12:构建资源
# Navigate to package
cd packages/Webkul/CustomTheme

# Install dependencies
npm install

# Build assets for production
npm run build

这将编译主题资源并创建构建目录。构建完成后,你的自定义主题即可使用,其中包含你创建的自定义主页。

Vite 驱动的资源

步骤 1:创建资源配置文件

在软件包根目录中创建:

文件: package.json

{
    "name": "custom-theme",
    "private": true,
    "description": "Custom Theme Package for Bagisto",
    "scripts": {
        "dev": "vite",
        "build": "vite build"
    },
    "devDependencies": {
        "autoprefixer": "^10.4.14",
        "axios": "^1.1.2",
        "laravel-vite-plugin": "^0.7.2",
        "postcss": "^8.4.23",
        "tailwindcss": "^3.3.2",
        "vite": "^4.0.0"
    }
}

文件: vite.config.js

import { defineConfig, loadEnv } from "vite";
import laravel from "laravel-vite-plugin";
import path from "path";

export default defineConfig(({ mode }) => {
    const envDir = "../../../";

    Object.assign(process.env, loadEnv(mode, envDir));

    return {
        build: {
            emptyOutDir: true,
        },
        envDir,
        server: {
            host: process.env.VITE_HOST || "localhost",
            port: process.env.VITE_PORT || 5173,
            cors: true,
        },
        plugins: [
            laravel({
                hotFile: "../../../public/custom-theme-vite.hot",
                publicDirectory: "../../../public",
                buildDirectory: "themes/custom-theme/build",
                input: [
                    "src/Resources/assets/css/app.css",
                    "src/Resources/assets/js/app.js",
                ],
                refresh: true,
            }),
        ],
    };
});

文件: tailwind.config.js

module.exports = {
    content: [
        "./src/Resources/**/*.blade.php",
        "../../../resources/themes/custom-theme/**/*.blade.php"
    ],
    theme: {
        container: {
            center: true,
            screens: { "2xl": "1440px" },
            padding: { DEFAULT: "90px" },
        },
        screens: {
            sm: "525px",
            md: "768px",
            lg: "1024px",
            xl: "1240px",
            "2xl": "1440px",
            1180: "1180px",
            1060: "1060px",
            991: "991px",
        },
        extend: {
            colors: {
                navyBlue: "#060C3B",
                darkGreen: "#40994A",
            },
            fontFamily: {
                poppins: ["Poppins"],
                dmserif: ["DM Serif Display"],
            },
        },
    },
    plugins: [],
    safelist: [{ pattern: /icon-/ }],
};

文件: postcss.config.js

module.exports = {
    plugins: {
        tailwindcss: {},
        autoprefixer: {},
    },
}
步骤 2:添加到 Bagisto Vite 配置

文件: config/bagisto-vite.php

'custom-theme' => [
    'hot_file' => 'custom-theme-vite.hot',
    'build_directory' => 'themes/custom-theme/build',
    'package_assets_directory' => 'src/Resources/assets',
],
开发命令
# Navigate to package
cd packages/Webkul/CustomTheme

# Install dependencies
npm install

# Start dev server with hot reload
npm run dev

# Build for production
npm run build

开发工作流

选项 A:符号链接(推荐)

创建符号链接,即可实时开发,无需重新发布:

# Remove published views
rm -rf resources/themes/custom-theme/views

# Create symlink from package to resources
ln -s $(pwd)/packages/Webkul/CustomTheme/src/Resources/views resources/themes/custom-theme/views
选项 B:直接开发软件包

直接在软件包中开发,并在需要时重新发布:

# After making changes
php artisan vendor:publish --provider="Webkul\CustomTheme\Providers\CustomThemeServiceProvider" --force

商城布局

要编写 Blade 本身? blade-conventions 技能包含标记规则 — : 与 :: 属性绑定、匿名组件与 Vue 支持的组件、缩进、注释 样式,以及翻译和 view_render_event 钩子的放置位置。

使用商城布局
<x-shop::layouts>
    <x-slot:title>
        Page Title
    </x-slot>

    {{-- Page content --}}
</x-shop::layouts>
布局属性
属性类型默认值描述
has-headerBooleantrue包含页眉导航
has-featureBooleantrue显示特色版块
has-footerBooleantrue包含页脚
最简页面示例
<x-shop::layouts
    :has-header="false"
    :has-footer="false"
>
    <x-slot:title>
        Minimal Page
    </x-slot>

    {{-- Content without header/footer --}}
</x-shop::layouts>

Shop Blade 组件

可用组件
组件用途描述
<x-shop::accordion>可折叠版块切换内容的显示状态
<x-shop::breadcrumbs>导航路径显示当前页面路径
<x-shop::button>操作按钮支持加载状态
<x-shop::datagrid>数据表格支持排序、筛选和分页
<x-shop::drawer>滑出面板位置:顶部/底部/左侧/右侧
<x-shop::dropdown>下拉菜单位置:左上、右下等
<x-shop::flat-picker.date>日期选择器基于 Flatpickr
<x-shop::flat-picker.datetime>日期时间选择器基于 Flatpickr
<x-shop::media.images>图片上传支持多张图片
<x-shop::modal>对话框提供页眉、内容和页脚插槽
<x-shop::quantity-changer>数量输入框+/- 按钮
<x-shop::table>数据表格可自定义 thead/tbody
<x-shop::tabs>标签导航位置:左侧/右侧/居中
<x-shop::shimmer.*>加载效果骨架屏

自定义布局

创建自定义布局

文件: packages/Webkul/CustomTheme/src/Resources/views/layouts/master.blade.php

<!DOCTYPE html>
<html lang="{{ app()->getLocale() }}">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>{{ $title ?? config('app.name') }}</title>
    
    {{-- Load assets manually for custom layouts --}}
    @bagistoVite([
        'src/Resources/assets/css/app.css',
        'src/Resources/assets/js/app.js'
    ])
</head>
<body>
    @if($hasHeader ?? true)
        @include('custom-theme::layouts.header')
    @endif
    
    <main>
        {{ $slot }}
    </main>
    
    @if($hasFooter ?? true)
        @include('custom-theme::layouts.footer')
    @endif
</body>
</html>

完整的软件包结构

packages/Webkul/CustomTheme/
├── src/
│   ├── Providers/
│   │   └── CustomThemeServiceProvider.php
│   └── Resources/
│       ├── assets/
│       │   ├── css/
│       │   │   └── app.css
│       │   ├── js/
│       │   │   └── app.js
│       │   ├── images/
│       │   └── fonts/
│       └── views/
│           ├── layouts/
│           │   └── master.blade.php
│           ├── home/
│           │   └── index.blade.php
│           ├── components/
│           └── ...
├── package.json
├── vite.config.js
├── tailwind.config.js
└── postcss.config.js

关键文件参考

文件用途
config/themes.php主题配置
config/bagisto-vite.phpVite 资源配置
packages/Webkul/Shop/src/Providers/ShopServiceProvider.phpShop 软件包注册
packages/Webkul/Shop/src/Http/Middleware/Theme.php主题解析
packages/Webkul/Theme/src/Themes.php主题门面
packages/Webkul/Shop/src/Resources/views/components/*Shop 组件

常见问题

  • 更改主题配置后未清除缓存
  • 注册软件包后忘记运行 composer dump-autoload
  • 未复制完整的 Shop 资源(视图和资源文件)
  • 使用自定义布局时,未手动加载 @bagistoVite 资源
  • 在已发布的文件中工作,而不是在软件包源文件中工作
  • 开发工作流缺少符号链接设置

测试

通过以下方式测试主题:

  1. 在渠道设置中激活主题
  2. 访问店面页面
  3. 检查响应式设计
  4. 验证所有 Shop 功能是否正常
  5. 在开发期间测试热重载

评论

支持 Markdown