darkiron
darkiron

Reputation: 1234

Sylius/Symfony 3 inject service in a service

I created a service to extend the menu in admin of 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

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!

Upvotes: 0

Views: 303

Answers (2)

Andrey V.
Andrey V.

Reputation: 72

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.

For example:

for uri

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

for route

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

Upvotes: 1

Fabien Salles
Fabien Salles

Reputation: 1191

If you use autowire to true you don't need to specify the router service. 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.

Upvotes: 0

Related Questions