简体   繁体   English

Sylius / Symfony 3在服务中注入服务

[英]Sylius/Symfony 3 inject service in a service

I created a service to extend the menu in admin of Sylius. 我创建了一个服务来扩展Sylius管理员的菜单。 It's work well ;) I follow the official doc 运行良好;) 我遵循官方文档

I try to inject the router service in, but I've this following error : 我尝试注入路由器服务,但是出现以下错误:

Type error: Too few arguments to function XXMenuListener::__construct(), 0 passed in appDevDebugProjectContainer.php on line 1542 and exactly 1 expected 类型错误:函数XXMenuListener :: __ construct()的参数太少,在1542行的appDevDebugProjectContainer.php中传递了0,并且正好是1

The declaration of this service : 这项服务的声明:

services:
    app.listener.admin.menu_builder:
        class: XXX\Menu\AdminMenuListener
        autowire: true
        arguments:
            - '@router'
        tags:
            - { name: kernel.event_listener, event: sylius.menu.admin.main, method: addAdminMenuItems }

and the service himself : 服务本身:

<?php

namespace XXX\Menu;

use Sylius\Bundle\UiBundle\Menu\Event\MenuBuilderEvent;
use Symfony\Bundle\FrameworkBundle\Routing\Router;

final class AdminMenuListener
{

    private $router;

    public function __construct(Router $router){
        $this->router = $router;
    }


    /**
     * @param MenuBuilderEvent $event

     */
    public function addAdminMenuItems(MenuBuilderEvent $event){
        $menu = $event->getMenu();

        $newSubmenu = $menu
            ->addChild('new')
            ->setLabel('XXX')
        ;

        $newSubmenu
            ->addChild('new-subitem')
            ->setLabel('XXX')
            //->setUri('https://www.google.com');
            ->setUri($this->router->generate('foo'))
        ;
    }
}

What is wrong in ? 有什么问题吗? Thanks for your help! 谢谢你的帮助!

I think you need to clear cache if not helped to clean the cache directory manually. 我认为,如果不帮助手动清除缓存目录,则需要清除缓存。 In any case, you don't need a router service because menubuilder already has it. 无论如何,您都不需要路由器服务,因为menubuilder已经具备了。

For example: 例如:

for uri 对于uri

$newSubmenu
   ->addChild('new-subitem')
   ->setLabel('XXX')
   ->setUri('https://www.google.com')
;

for route 路线

$newSubmenu
   ->addChild('new-subitem', ['route' => 'foo'])
   ->setLabel('XXX')
;

If you use autowire to true you don't need to specify the router service. 如果将autowiretrue ,则无需指定路由器服务。 Something like this should be enough : 像这样的东西就足够了:

services:
    app.listener.admin.menu_builder:
        class: XXX\Menu\AdminMenuListener
        autowire: true
        tags:
            - { name: kernel.event_listener, event: sylius.menu.admin.main, method: addAdminMenuItems }

In any case, your error indicates that you don't have any arguments. 无论如何,您的错误都表明您没有任何参数。 May be it's a caching issue or may be you have another service declaration for the same class XXX\\Menu\\AdminMenuListener without autowire to true and without arguments. 可能是缓存问题,或者您是同一类XXX\\Menu\\AdminMenuListener另一个服务声明,而没有自动装配为true且没有参数。

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

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