简体   繁体   English

如何扩展 Shopware 6 controller 操作

[英]How to extend Shopware 6 controller action

I am building a plugin for Shopware 6 and cannot seem to find any documentation as to how to extend an existing controller action.我正在为 Shopware 6 构建一个插件,但似乎找不到任何关于如何扩展现有 controller 操作的文档。 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 中找到了如何向帐户 Controller 添加操作,但它似乎指的是 Shopware 5,我不确定是否可以在 Shopware 6 中以这种方式使用它。

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.我想扩展的 controller 操作是\Shopware\Storefront\Controller\AddressController::saveAddress - 在我的情况下,我想添加自定义地址验证,该验证将在我的插件中使用服务,其中将向第 3 方 API 发出请求,如果地址正确则允许该地址,否则返回错误。 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).也许最好改为扩展\Shopware\Core\Checkout\Customer\SalesChannel\AddressService::save但我现在不知道(我一般是 Shopware 的新手)。 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).或者也许有一个事件可以用来保存地址(同样的,找不到适合 Shopware6 的事件源/列表)。

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 .这里似乎有指南: https://docs.shopware.com/en/shopware-platform-dev-en/developer-guide/controller和这里: https://docs.shopware.com/en/shopware-platform -dev-en/how-to/custom-storefront-controller但这些仅描述了如何制作新的 controller 对我来说不是很有用,因为我不想添加任何新路线但使用现有的/account/address/create

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.我将非常感谢有关如何在插件(配置、xml)中注册覆盖以及扩展 class 的外观的代码示例。 If it is not too much to ask the ideal answer would contain an example of:如果问的不是太多,理想的答案将包含以下示例:

  1. How to extend an action for existing controller.如何扩展现有 controller 的操作。
  2. How to extend an existing service.如何扩展现有服务。
  3. Where to find which event is firing in a controller/service, subscribe to it and make it override default behaviour (like throw Shopware\Core\Framework\Validation\Exception\ConstraintViolationException ).在哪里可以找到控制器/服务中触发的事件,订阅它并使其覆盖默认行为(如 throw Shopware\Core\Framework\Validation\Exception\ConstraintViolationException )。
  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.扩展/覆盖操作是没有意义的,因为操作应该尽可能地精简,并且所有业务逻辑都应该在服务中。

  2. 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请参阅https://docs.shopware.com/en/shopware-platform-dev-en/how-to/decorating-a-service

  3. In your case, you can subscribe on framework.validation.address.create or/and framework.validation.address.update events to extend list of constraints.在您的情况下,您可以订阅framework.validation.address.create或/和framework.validation.address.update事件以扩展约束列表。 In general all validation events have prefix framework.validation.一般来说,所有验证事件都有前缀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\Core\Framework\Validation\DataValidationFactoryInterface实现中定义,在您的情况下它是\Shopware\Core\Checkout\Customer\Validation\AddressValidationFactory

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM