sylius/shop-api-plugin

Sylius电子商务平台的Shop API。

安装次数: 281 102

依赖: 1

建议者: 0

安全性: 0

星级: 130

关注者: 30

分支: 89

开放问题: 50

类型:sylius-plugin


README

Sylius Shop API

License Build Status Scrutinizer Quality Score

此仓库包含一个插件,该插件通过JSON API扩展了Sylius电子商务平台,允许从客户的角度执行所有标准商店操作。

文档

最新的文档可在此处找到。如果您想了解更多关于系统如何工作的信息,请查看食谱

安装

重要提示:在安装SyliusShopApiPlugin之前,您应禁用所有SyliusShopBundle的依赖项。您不能一起使用这些包。

  1. 运行composer require sylius/shop-api-plugin,当被问及是否要执行Flex配方时,回答'是'。

  2. 扩展配置文件

    1. 将SyliusShopApi添加到config/bundles.php
    // config/bundles.php
    
        return [
            Sylius\ShopApiPlugin\SyliusShopApiPlugin::class => ['all' => true],
        ];
    1. - { path: '^/shop-api', priorities: ['json'], fallback_format: json, prefer_extension: true }添加到config/packages/fos_rest.yaml文件中的fos_rest.format_listener.rules部分,并从插件导入配置。
    # config/packages/_sylius_shop_api.yaml
    
    imports: # <-- Add this section if it does not already exist and add the lines below
        # ...
        - { resource: "@SyliusShopApiPlugin/Resources/config/app/config.yml" }
        - { resource: "@SyliusShopApiPlugin/Resources/config/app/sylius_mailer.yml" }
    
    # config/packages/fos_rest.yaml
    
    fos_rest:
        # ...
        
        format_listener:
            rules:
                - { path: '^/shop-api', priorities: ['json'], fallback_format: json, prefer_extension: true } # <-- Add this
                - { path: '^/api', priorities: ['json', 'xml'], fallback_format: json, prefer_extension: true }
                - { path: '^/', stop: true }
    
    1. 添加新的路由文件以导入SyliusShopApiPlugin的路由
    # config/routes/sylius_shop_api.yaml
    
    sylius_shop_api:
        resource: "@SyliusShopApiPlugin/Resources/config/routing.yml"
    1. 配置防火墙

      1. sylius.security.shop_regex参数更改为排除shop-api前缀
      2. 添加ShopAPI正则表达式参数sylius_shop_api.security.regex: "^/shop-api"
      3. 添加ShopAPI防火墙配置
        • Symfony 6

          # config/packages/security.yaml
          
          parameters:
              # ...
          
              sylius.security.shop_regex: "^/(?!admin|api/.*|api$|shop-api|media/.*)[^/]++" # shop-api has been added inside the brackets
              sylius_shop_api.security.regex: "^/shop-api"
          
          # ... 
          
          security:
              firewalls:
                  // ...
          
                  sylius_shop_api:
                      pattern: "%sylius_shop_api.security.regex%"
                      stateless: true
                      entry_point: jwt
                      provider: sylius_shop_user_provider
                      json_login:
                          check_path: /shop-api/login
                          username_path: email
                          password_path: password
                          success_handler: lexik_jwt_authentication.handler.authentication_success
                          failure_handler: lexik_jwt_authentication.handler.authentication_failure
                      jwt: true
             access_control:
             - { path: "%sylius_shop_api.security.regex%/address-book", role: ROLE_USER}
             - { path: "%sylius_shop_api.security.regex%/me", role: ROLE_USER}
        • Symfony 5

          # config/packages/security.yaml
          
          parameters:
              # ...
          
              sylius.security.shop_regex: "^/(?!admin|api/.*|api$|shop-api|media/.*)[^/]++" # shop-api has been added inside the brackets
              sylius_shop_api.security.regex: "^/shop-api"
          
          # ... 
          
          security:
              firewalls:
                  // ...
          
                  sylius_shop_api:
                      pattern: "%sylius_shop_api.security.regex%"
                      stateless: true
                      anonymous: true
                      provider: sylius_shop_user_provider
                      json_login:
                          check_path: /shop-api/login
                          username_path: email
                          password_path: password
                          success_handler: lexik_jwt_authentication.handler.authentication_success
                          failure_handler: lexik_jwt_authentication.handler.authentication_failure
                      guard:
                          authenticators:
                              - lexik_jwt_authentication.jwt_token_authenticator
             access_control:
             - { path: "%sylius_shop_api.security.regex%/address-book", role: ROLE_USER}
             - { path: "%sylius_shop_api.security.regex%/me", role: ROLE_USER}
    2. (可选)如果您已安装nelmio/NelmioCorsBundle以支持跨源Ajax请求

      1. 将NelmioCorsBundle添加到AppKernel
      // config/bundles.php
      
      return [
          Nelmio\CorsBundle\NelmioCorsBundle::class => ['all' => true],
      ];
      1. 添加新的配置文件
      # config/packages/nelmio_cors.yml
      
      # ...
      
      nelmio_cors:
          defaults:
              allow_credentials: false
              allow_origin: []
              allow_headers: []
              allow_methods: []
              expose_headers: []
              max_age: 0
              hosts: []
              origin_regex: false
              forced_allow_origin_value: ~
          paths:
              '^/shop-api/':
                  allow_origin: ['*']
                  allow_headers: ['Content-Type', 'authorization']
                  allow_methods: ['POST', 'PUT', 'GET', 'DELETE', 'PATCH', 'OPTIONS']
                  max_age: 3600
  3. 遵循https://github.com/lexik/LexikJWTAuthenticationBundle/blob/master/Resources/doc/index.rst#installation

Shop API的示例配置可在此找到:https://github.com/Sylius/SyliusDemo/commit/4872350dcd6c987d54dec1f365b4bb890d7183c9

附加功能

属性

如果您希望接收序列化属性,您需要在sylius_shop_api.included_attributes键下定义它们的代码数组。例如。

# config/packages/sylius_shop_api.yml
sylius_shop_api:
    included_attributes:
        - "MUG_MATERIAL_CODE"

此插件包含与LexikJWTAuthenticationBundle的集成。有关安全自定义的更多信息,请参阅那里。

测试

可以使用API测试用例测试应用程序。要运行测试套件,请执行以下命令

$ cp tests/Application/.env.test.dist tests/Application/.env.test
$ set -a && source tests/Application/.env.test && set +a
$ (cd tests/Application && bin/console doctrine:database:create -e test)
$ (cd tests/Application && bin/console doctrine:schema:create -e test)

$ vendor/bin/phpunit

应用程序也可以使用PHPSpec进行测试

$ vendor/bin/phpspec run

安全问题

如果您认为您发现了一个安全问题,请不要使用问题跟踪器,也不要公开发布。相反,所有安全问题都必须发送到security@sylius.com

维护

此库由Sylius以及以下组织外部的贡献者共同维护

发布周期

此项目遵循语义版本控制。Shop API的发布周期独立于Sylius的发布周期。

尚未计划下一主要版本发布。根据需要发布小版本和补丁版本。

我们只为最新的小版本提供bug修复。对于后续小版本的发布,我们提供一年的安全修复。

支持版本