如何在 Sylius 中动态生成服务的工作机制?

How does work mechanics to dynamic generate services in Sylius?

工作机制如何在 Sylius 中动态生成服务,然后可以像 services.yml 文件中的方法 get() 中配置的普通服务一样使用这些服务。

我说一下这个情况:

扩展基础模型 所有 Sylius 模型与接口一起位于 Sylius\Component\Xyz\Model 命名空间中。例如,对于 Sylius 税收组件,它是 TaxCategory 和 TaxRate。

假设您要将“区域”字段添加到 Sylius 税率。

首先,您需要创建自己的 TaxRate class,这将扩展基本模型。

命名空间 Acme\Bundle\ShopBundle\Entity;</p> <p>使用Sylius\Component\Addressing\Model\ZoneInterface; 使用 Sylius\Component\Taxation\Model\TaxRate 作为 BaseTaxRate;</p> <p>class TaxRate 扩展了 BaseTaxRate { 私人$区;</p> <pre><code>public function getZone() { return $this->zone; } public function setZone(ZoneInterface $zone) { $this->zone = $zone; return $this; }

}

最后,您在 app/config/config.yml 文件中配置 class。

sylius_taxation: driver: doctrine/orm classes: tax_rate: model: Acme\ShopBundle\Entity\TaxRate # Your tax rate entity.

Sylius 像正常服务一样自动生成对 class 的访问权限。

效果如何?是捆绑包吗?这是非常好的解决方案。我想在我自己的 Symfony2 项目中实现这些机制。

如果您想在自己的项目中使用它,只需通过 composer 集成 SyliusResourceBundle。现在您可以使用给定的 类.

在配置中定义资源
sylius_resource:
    resources:
        app.entity_name:
            driver: doctrine/orm
            object_manager: default
            templates: App:User
            classes:
                model: App\Entity\EntityName
                interface: App\Entity\EntityNameInterface

现在可用的服务是 app.model.entity_nameapp.repository.entity_nameapp.manager.entity_nameapp.controller.entity_name

http://docs.sylius.org/en/latest/bundles/SyliusResourceBundle/configuration.html