loevgaard/sylius-brand-plugin

一个用于处理品牌的Sylius插件

资助包维护!
Setono

安装次数: 125,216

依赖者: 2

建议者: 1

安全: 0

星标: 30

关注者: 5

分支: 29

公开问题: 18

类型:sylius-plugin


README

Latest Version on Packagist Software License Build Status Quality Score

如果您想为您的产品添加品牌,请使用此插件。使用案例

  • 将品牌标志添加到您的产品页面
  • 在前端或后端按品牌过滤,例如产品列表

截图

点击查看

菜单

Screenshot showing admin menu

品牌管理页面

Screenshot showing brand admin index page

Screenshot showing brand admin update page

Screenshot showing brand admin media tab at update page

产品管理页面

Screenshot showing product admin index page with brand filter

Screenshot showing product admin index page with brand column

Screenshot showing brand tab at product admin update page

安装

步骤 1:下载插件

打开命令行,进入您的项目目录,然后执行以下命令以下载此捆绑包的最新稳定版本

$ composer require loevgaard/sylius-brand-plugin

此命令要求您全局安装Composer,如Composer文档的安装章节中所述。

步骤 2:启用插件

然后,通过将其添加到项目中 config/bundles.php 文件中注册的插件/捆绑包列表中,在 SyliusGridBundle 之前(!)启用插件

<?php

# config/bundles.php

return [
    // ...
    Loevgaard\SyliusBrandPlugin\LoevgaardSyliusBrandPlugin::class => ['all' => true],
    Sylius\Bundle\GridBundle\SyliusGridBundle::class => ['all' => true],
    // ...
];

步骤 3:配置插件

# config/packages/loevgaard_sylius_brand.yaml

imports:
    # ...
    - { resource: "@LoevgaardSyliusBrandPlugin/Resources/config/app/config.yaml" }

    # If you want to see Brand column at admin products list - uncomment next line
    # - { resource: "@LoevgaardSyliusBrandPlugin/Resources/config/grids/sylius_admin_product.yaml" }
# config/routes/loevgaard_sylius_brand.yaml

loevgaard_sylius_brand:
    resource: "@LoevgaardSyliusBrandPlugin/Resources/config/routing.yaml"

步骤 4:扩展服务和实体

扩展 Product

  • 如果您使用 annotations 映射

    <?php
    
    declare(strict_types=1);
    
    namespace App\Entity;
    
    use Doctrine\ORM\Mapping as ORM;
    use Loevgaard\SyliusBrandPlugin\Model\ProductInterface as LoevgaardSyliusBrandPluginProductInterface;
    use Loevgaard\SyliusBrandPlugin\Model\ProductTrait as LoevgaardSyliusBrandPluginProductTrait;
    use Sylius\Component\Core\Model\Product as BaseProduct;
    
    /**
     * @ORM\Entity
     * @ORM\Table(name="sylius_product")
     */
    class Product extends BaseProduct implements LoevgaardSyliusBrandPluginProductInterface
    {
        use LoevgaardSyliusBrandPluginProductTrait;
    }
  • 如果您使用 xml 映射

    <?php
    // src/Model/Product.php
    
    declare(strict_types=1);
    
    namespace App\Model;
    
    use Loevgaard\SyliusBrandPlugin\Model\ProductInterface as LoevgaardSyliusBrandPluginProductInterface;
    use Loevgaard\SyliusBrandPlugin\Model\ProductTrait as LoevgaardSyliusBrandPluginProductTrait;
    use Sylius\Component\Core\Model\Product as BaseProduct;
    
    class Product extends BaseProduct implements LoevgaardSyliusBrandPluginProductInterface
    {
        use LoevgaardSyliusBrandPluginProductTrait;
        
        // ...
    }
    <?xml version="1.0" encoding="UTF-8"?>
    
    <!-- config/doctrine/model/Product.orm.xml -->
    
    <doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
                      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                      xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
                                          http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
    
        <mapped-superclass name="App\Model\Product" table="sylius_product">
            <many-to-one field="brand" target-entity="Loevgaard\SyliusBrandPlugin\Model\BrandInterface" inversed-by="products">
                <join-column name="brand_id" on-delete="SET NULL" />
            </many-to-one>
        </mapped-superclass>
    
    </doctrine-mapping>

扩展 ProductRepository

<?php
# src/Doctrine/ORM/ProductRepository.php

declare(strict_types=1);

namespace App\Doctrine\ORM;

use Loevgaard\SyliusBrandPlugin\Doctrine\ORM\ProductRepositoryInterface as LoevgaardSyliusBrandPluginProductRepositoryInterface;
use Loevgaard\SyliusBrandPlugin\Doctrine\ORM\ProductRepositoryTrait as LoevgaardSyliusBrandPluginProductRepositoryTrait;
use Sylius\Bundle\CoreBundle\Doctrine\ORM\ProductRepository as BaseProductRepository;

class ProductRepository extends BaseProductRepository implements LoevgaardSyliusBrandPluginProductRepositoryInterface
{
    use LoevgaardSyliusBrandPluginProductRepositoryTrait;

    // ...
}

配置

config/services.yaml

sylius_product:
    resources:
        product:
            classes:
                model: App\Model\Product # Or App\Entity\Product
                repository: App\Doctrine\ORM\ProductRepository

步骤 5:更新您的数据库模式

$ php bin/console doctrine:migrations:diff
$ php bin/console doctrine:migrations:migrate

固定数据

  • 包含预先定义的品牌固定数据以在您的开发环境中使用

    # config/packages/loevgaard_sylius_brand.yaml
    
    imports:
        - { resource: "@LoevgaardSyliusBrandPlugin/Resources/config/app/fixtures.yaml" }
  • 或编写您自己的

    • config/packages 文件夹中添加一个新的 yaml 文件,并将其命名为您想要的名称,例如 my_own_fixtures.yaml

    • 在此 yaml 中填写您自己的品牌固定数据,并且不要忘记在此品牌定义之前声明您的产品定义,或者使用现有产品代码。

      # config/packages/my_own_fixtures.yaml
      
      sylius_fixtures:
         suites:
             my_own_brand_fixtures:
                  fixtures:
                      loevgaard_sylius_brand_plugin_brand:
                          options:
                              custom:
                                  my_brand:
                                      name: 'My brand'
                                      code: 'my-brand'
                                      images:
                                          local_image:
                                              type: logo
                                              path: images/my-brand/logo.jpg
                                          3rd_party_plugin_image:
                                              type: black-and-white
                                              path: '@SomePlugin/Resources/images/my-brand/black-and-white.jpg'
                                      products:
                                        - product_code_1
                                        - product_code_2
                                        - product_code_3
      

      请参阅 src/Resources/config/app/fixtures.yaml 中的示例。

  1. 加载您的固定数据

    php bin/console sylius:fixture:load my_own_brand_fixtures

插件开发和安装及使用

要运行测试应用程序进行交互,只需输入 composer try

可用的Sonata块

  • loevgaard_sylius_brand.admin.brand.create.tab_details
  • loevgaard_sylius_brand.admin.brand.update.tab_details
  • loevgaard_sylius_brand.admin.brand.create.tab_media
  • loevgaard_sylius_brand.admin.brand.update.tab_media

可用事件

  • loevgaard_sylius_brand.menu.admin.brand.form 以自定义 Brand 管理表单,就像您通常通过 sylius.menu.admin.product.form 事件自定义 Product 表单一样。

测试

与API玩耍

  • 安装 https://stedolan.github.io/jq/(在OSX终端中输入 brew install jq

  • 获取管理员API访问令牌

    SYLIUS_ADMIN_API_ACCESS_TOKEN=$(curl https://:8000/api/oauth/v2/token \
        --silent --show-error \
        -d "client_id"=demo_client \
        -d "client_secret"=secret_demo_client \
        -d "grant_type"=password \
        -d "username"=api@example.com \
        -d "password"=sylius-api | jq '.access_token' --raw-output)
  • 进行请求

    (以下示例中的 GET 请求索引/显示资源,有关创建/更新/删除与品牌相关的资源、创建附加品牌的新产品、通过API上传图像文件的详细信息,请参阅 tests/Controller/*ApiTest.php

    到品牌管理API

    curl https://:8000/api/v1/brands/ \
        -H "Authorization: Bearer $SYLIUS_ADMIN_API_ACCESS_TOKEN"
    curl https://:8000/api/v1/brands/setono/ \
        -H "Authorization: Bearer $SYLIUS_ADMIN_API_ACCESS_TOKEN"

    品牌图像API

    curl https://:8000/api/v1/brands/setono/images/ \
        -H "Authorization: Bearer $SYLIUS_ADMIN_API_ACCESS_TOKEN"

    按类型获取品牌图像API

    curl https://:8000/api/v1/brands/setono/images/logo/ \
        -H "Authorization: Bearer $SYLIUS_ADMIN_API_ACCESS_TOKEN"
    curl https://:8000/api/v1/brands/setono/images/<ID>/ \
        -H "Authorization: Bearer $SYLIUS_ADMIN_API_ACCESS_TOKEN"

    品牌产品API

    curl https://:8000/api/v1/brands/setono/products/ \
        -H "Authorization: Bearer $SYLIUS_ADMIN_API_ACCESS_TOKEN"

    产品详细信息API

    curl https://:8000/api/v1/products/setono-mug/ \
        -H "Authorization: Bearer $SYLIUS_ADMIN_API_ACCESS_TOKEN"

    对于XML输出,请将 -H "Accept: application/xml" 添加到请求中

贡献

请,在提交之前运行 composer all 以运行所有检查和测试。

通过翻译进行贡献

我们使用Sylius翻译的同一服务,即 Crowdin。您可以通过将此项目翻译成您的母语来提供帮助 ;)

谢谢!