dovstone
dovstone

Reputation: 144

Symfony 4: I can't autowire inside my bundle constructor

My controller PagesController located to vendor/dovstone/symfony-blog-admin/src/Controller contains this code:

<?php 

//...

class PagesController extends Controller 
{

    private $em;

    public function __construct(EntityManagerInterface $em)
    {
        $this->em = $em;
        dump($em) // return null;
    }

    // ...

Symfony throws me this: Type error: Too few arguments to function DovStone\Bundle\BlogAdminBundle\Controller\PagesController::__construct(), 0 passed in C:\Apps\Web\sf4\vendor\symfony\http-kernel\Controller\ControllerResolver.php on line 111 and exactly 1 expected.

What am I doing wrong?

Upvotes: 1

Views: 2667

Answers (2)

dovstone
dovstone

Reputation: 144

Here is what I've done:

<?php 

//...

class PagesController extends Controller 
{

protected $container;

private $em;

public function __construct(ContainerInterface $container, EntityManagerInterface $em)
{
    $this->container = $container;

    $this->em = $em;
    dump($em); // EntityManagerInterface;
}

// ...

After this, I had to clear the cache by running php bin/console cache:clear

Upvotes: 0

Alister Bulman
Alister Bulman

Reputation: 35169

In your local Controller directory, the autowiring/autoconfig is enabled by the Yaml configuration.

In a package, you are expected to explicitly list your dependencies in the configuration. This would be setup by reading the configuration from the bundle or bridge config.

The directory you show doesn't seem to be a bundle (it doesn't have a name that indicates it - but it might still have the appropriate files), so if it is a simple package (without the Symfony framework structure to define services), you would want to add another package that was a thin layer making services from a plain-php package that it depends on.

Upvotes: 1

Related Questions