You need to enable JavaScript to run this app. 重写Sylius 1.8.6 ShopApiPlugin用户注册时自定义逻辑未生效
优惠活动
大模型
产品
解决方案
定价
更多

重写Sylius 1.8.6 ShopApiPlugin用户注册时自定义逻辑未生效

解决方案

你的自定义类完全没被触发,核心原因有三个,逐一修正即可:

  1. 路由绑定的Request未替换
    原/register路由默认绑定系统自带的RegisterCustomerRequest,你仅定义了自定义Request类但未修改路由配置,请求根本不会进入自定义逻辑。
    找到config/routes/sylius_shop_api.yaml下的注册路由,修改sylius.api.request_class的值为你的自定义Request全限定名:
    sylius_shop_api_register_customer:
        path: /register
        methods: [POST]
        controller: sylius.controller.shop_api:registerCustomerAction
        defaults:
            sylius.api.request_class: App\Controller\ShopAPI\Requests\UserRegistration
    
  2. 服务配置错误
    • 删除所有无效配置项:
      • 删掉App\Controller\ShopAPI\Commands\UserRegistrationCommand的服务配置:Command是在Request中手动实例化的,不需要容器注入参数,%email%为不存在的占位符。
      • 删掉Sylius\ShopApiPlugin\Command\Customer\RegisterCustomer的类覆盖配置:该值是Command类的全限定名,不是Handler的服务ID,这种覆盖方式不符合Symfony Messenger的消息绑定规则。
    • 修正Handler的服务配置,通过messenger.message_handler标签显式指定它处理的Command类:
      App\Controller\ShopAPI\Handlers\UserRegistrationHandler:
          public: true
          arguments:
              $userRepository: '@sylius.repository.shop_user'
              $userFactory: '@sylius.factory.shop_user'
              $customerProvider: '@sylius.shop_api_plugin.provider.customer_provider'
              # 补全缺失的依赖参数
              $addressRepository: '@sylius.repository.address'
              $addressFactory: '@sylius.factory.address'
              $channelRepository: '@sylius.repository.channel'
              $eventDispatcher: '@event_dispatcher'
          tags:
              - { name: messenger.message_handler, handles: App\Controller\ShopAPI\Commands\UserRegistrationCommand }
      
    • 修正自定义Request的参数配置,不要使用不存在的%channelCode%占位符,从当前渠道上下文动态获取:
      App\Controller\ShopAPI\Requests\UserRegistration:
          arguments:
              $channelCode: '@=service("sylius.context.channel").getCode()'
      
  3. 代码细节修正
    • 删掉Request构造函数里的var_dump和die调试代码,避免请求直接终止。
    • 将Handler里的$address->setCustomer($user)改为$address->setCustomer($customer),Address的关联字段需要传Customer对象,而非ShopUser对象,否则会触发类型错误。
    • 如果permit字段不是必填项,修改实体的ORM注解为nullable=true,避免用户未传值时报数据库错误;如果为必填项,在Request层增加参数校验,不符合要求直接返回400错误。

修改完成后清空缓存重新测试,即可命中自定义逻辑。


内容的提问来源于stack exchange,提问作者Dennis de Best

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.11.12 02:15:02