0

如何用 XML 写这个?

# src/Acme/MainBundle/Resources/config/services.yml
services:
    acme_main.menu_builder:
        class: Acme\MainBundle\Menu\MenuBuilder
        arguments: ["@knp_menu.factory"]

    acme_main.menu.main:
        class: Knp\Menu\MenuItem # the service definition requires setting the class
        factory_service: acme_main.menu_builder
        factory_method: createMainMenu
        arguments: ["@request"]
        scope: request # needed as we have the request as a dependency here
        tags:
            - { name: knp_menu.menu, alias: main } # The alias is what is used to retrieve the menu

我已经尝试过了,但出现下一个错误:xml:

<!-- Knp Menu -->
<service id="acme.menu_builder" class="Acme\FrontendBundle\Menu\MenuBuilder">
    <argument type="service" id="knp_menu.factory" />
    <argument type="service" id="security.context" />
</service>

<service id="acme.menu.main" class="Acme\Menu\MenuItem" scope="request">
    <argument type="service" id="acme_menu.menu_builder" />
    <argument type="service" id="request" />
    <tag name="knp_menu.menu" alias="main" method="createMainMenu" />
</service>

错误: ErrorException: Catchable Fatal Error: Argument 2 passed to Knp\Menu\MenuItem::__construct() must implement interface Knp\Menu\FactoryInterface, instance of Symfony\Component\HttpFoundation\Request given

4

1 回答 1

3

你从哪里得到 XML 格式的?它看起来像一些完全不同的设置(例如,在 yaml 中acme.menu_builderAcme\MainBundle\Menu\MenuBuilder类,Acme\FrontendBundle\Menu\MenuBuilder在 xml 中有类。acme.menu_builder服务在 yaml 中有 1 个参数,在 xml 中有 2 个参数?

要回答您的问题,它应该如下所示:

<services>
    <service id="acme_main.menu_builder" 
        class="Acme\MainBundle\Menu\MenuBuilder"
    >
        <argument type="service" id="knp_menu.factory" />
    </service>

    <service id="acme_main.menu.main"
        class="Knp\Menu\MenuItem"
        factory-service="acme_main.menu_builder"
        factory-method="createMainMenu"
        scope="request"
    >
        <argument type="service" id="request" />
        <tag name="knp_menu.menu" alias="main" />
    </service>
</services>
于 2013-01-02T13:38:39.857 回答