0
votes

In Symfony 3.4 I have a controller, which has some parameters filled from the URL and also I need a service to be injected. I get the following error message:

Controller "Acs\EventNodeBundle\Controller\MainController::calendarAction()" requires that you provide a value for the "$router" argument. Either the argument is nullable and no null value has been provided, no default value has been provided or because there is a non optional argument after this one.

Why don't Symfony injects the Router? This is my code:

The controller:

...

use Symfony\Bundle\FrameworkBundle\Routing\Router;

/**
 * Class MainController
 */
class MainController extends Controller
{
    /**
     * @param Router   $router
     * @param null|int $year
     * @param null|int $month
     * @return RedirectResponse|Response
     */
    public function calendarAction(Router $router, $year = null, $month = null)
    {
...

The services.yml:

...
services:
    # default configuration for services in *this* file
    _defaults:
        # automatically injects dependencies in services
        autowire: true
        # automatically registers services as commands, event subscribers, etc.
        autoconfigure: true
        # services cannot be fetched directly from the container via $container->get()
        # need to override this setting individually
        public: false

        bind:
            $entityManager: '@Doctrine\ORM\EntityManagerInterface'

   # enable autowiring for controllers
   Acs\EventNodeBundle\Controller\:
       resource: '../../Controller'
       public: true
       tags: ['controller.service_arguments']
...

And the routing:

...
    eventnode_main:
        path: /{year}/{month}
        methods: [GET]
        defaults: { _controller: AcsEventNodeBundle:Main:calendar, year: null, month: null}
        requirements:
            year: '\d+'
            month: '\d+'
...
2

2 Answers

0
votes

A constructor in your Controller should do the job:

/**
 * MainController constructor.
 *
 * @param Router $router
 */
public function __construct(
    Router $router
) {
    $this->router = $router;
}
0
votes

Try to add this in your services.yml :

Acs\EventNodeBundle\:
    resource: '../../src/EventNodeBundle/*'

This will make classes in src/EventNodeBundle available to be used as services.

See this documentation : https://symfony.com/doc/3.4/service_container/3.3-di-changes.html