如何扩展Shopware 6控制器动作 [英] How to extend Shopware 6 controller action

查看:55
本文介绍了如何扩展Shopware 6控制器动作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为Shopware 6构建一个插件,但是似乎找不到有关如何扩展现有控制器操作的任何文档.我发现此如何在Shopware ,但它似乎是指Shopware 5,我不确定是否可以在Shopware 6中使用这种方式.

I am building a plugin for Shopware 6 and cannot seem to find any documentation as to how to extend an existing controller action. I found this How to add an Action to Account Controller in Shopware but it seems to refer to Shopware 5 and I am not sure I can use it that way in Shopware 6.

我要扩展的控制器操作是 \ Shopware \ Storefront \ Controller \ AddressController :: saveAddress -在我的情况下,我想添加自定义地址验证,该验证将在插件中使用服务将向第三方API发出请求,如果地址正确,则允许该地址,如果不正确,则返回错误.也许最好改为扩展 \ Shopware \ Core \ Checkout \ Customer \ SalesChannel \ AddressService :: save ,但目前我没有任何头绪(一般来说,我是Shopware的新手).扩展服务将意味着我不必重写整个操作逻辑,因此它包含我中间的检查.也许有一个事件我可以用来保存地址(同样的事情,找不到Shopware6的事件的良好来源/列表).

The controller action I want to extend is \Shopware\Storefront\Controller\AddressController::saveAddress - in my case I want to add custom address validation that would use a service in my plugin where a request to 3rd party API would be made, if the address is correct then allow the address, if not then return an error. Perhaps it is better to instead extend \Shopware\Core\Checkout\Customer\SalesChannel\AddressService::save but I have no clue for now (I am new to Shopware in general). Extending the service would mean I do not have to override the whole action logic so that it contains my check in the middle. Or perhaps there is an event I can use for address saving (same thing, can't find a good source/list of events for Shopware6).

这里似乎有一些指南: https://docs.shopware.com/zh-CN/shopware-platform-dev-zh/developer-guide/controller 和这里: https://docs.shopware.com/en/shopware-platform-dev-zh/how-to/custom-storefront-controller 但是这些仅描述了如何制作一个新的控制器,对我来说不是很有用,因为我不想添加任何新路由,而是使用现有的/account/address/create .

There seem to be guides here: https://docs.shopware.com/en/shopware-platform-dev-en/developer-guide/controller and here: https://docs.shopware.com/en/shopware-platform-dev-en/how-to/custom-storefront-controller but these only describe how to make a new controller and it is not very useful to me since I do not want to add any new routes but use the existing one /account/address/create.

对于一个如何在插件(config,xml)中注册替代代码以及扩展类的外观的代码示例,我将不胜感激.如果不是太多的问题,那么理想的答案将包含以下示例:

I would be very grateful for a code example of how to register the override in the plugin (config, xml) and how would the extending class look like. If it is not too much to ask the ideal answer would contain an example of:

  1. 如何扩展现有控制器的动作.
  2. 如何扩展现有服务.
  3. 在哪里找到控制器/服务中触发的事件,请订阅该事件并使其覆盖默认行为(例如引发 Shopware \ Core \ Framework \ Validation \ Exception \ ConstraintViolationException ).
  4. li>

推荐答案

  1. 扩展/覆盖操作没有意义,因为操作应尽可能的薄,并且所有业务逻辑都应投入使用.

  1. It does not make sense to extend/override action, as action should be as thin as it is possible, and all business logic should be in service.

要扩展现有服务,您可以装饰它或订阅一些事件以扩展功能.请参阅 https://docs.shopware.com/en/shopware-platform-dev-en/how-to/decorating-a-service

To extend existing service you can decorate it or subscribe to some events to extend functionality. See https://docs.shopware.com/en/shopware-platform-dev-en/how-to/decorating-a-service

在您的情况下,您可以订阅 framework.validation.address.create 或/和 framework.validation.address.update 事件以扩展列表约束.通常,所有验证事件的前缀都是 framework.validation.第二部分是在 \ Shopware \ Core \ Framework \ Validation \ DataValidationFactoryInterface 实现中定义的,对于您来说,它是 \Shopware \ Core \ Checkout \ Customer \ Validation \ AddressValidationFactory

In your case, you can subscribe on framework.validation.address.create or/and framework.validation.address.update events to extend list of constraints. In general all validation events have prefix framework.validation. second part is defined in \Shopware\Core\Framework\Validation\DataValidationFactoryInterface implementation in your case it is \Shopware\Core\Checkout\Customer\Validation\AddressValidationFactory

这篇关于如何扩展Shopware 6控制器动作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆