Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Cannot access the container from inside a controller #23200

Closed
@Aerendir

Description

@Aerendir
Q A
Bug report? yes
Feature request? no
BC Break report? no
Symfony version 3.3.2

I'm migrating my app from Symfony 2.8 to Symfony 3.3.

NOTE: I'm not upgrading the 2.8 version, but installed a fresh project of Symfony 3.3 and moving all relvant files to it from the old 2.8 Symfony project.

From inside a controller of mine I have this:

public function indexAction()
{
    $email = new Email();

    $form = $this->createForm(GetStartedType::class, $email, [
        'action' => $this->generateUrl('get_started_end'),
        'method' => 'POST',
    ]);

    return [
        'form' => $form->createView(),
    ];
}

But I receive this exception:

Call to a member function get() on null

My controller extends Symfony\Bundle\FrameworkBundle\Controller\Controller:

/**
 * {@inheritdoc}
 */
class DefaultController extends Controller
{
...
}

So I have access to the container.

Putting some dumps around in the Symfony's code, I see that the container is correctly set:

namespace Symfony\Component\DependencyInjection;

/**
 * ContainerAware trait.
 *
 * @author Fabien Potencier <[email protected]>
 */
trait ContainerAwareTrait
{
    /**
     * @var ContainerInterface
     */
    protected $container;

    /**
     * Sets the container.
     *
     * @param ContainerInterface|null $container A ContainerInterface instance or null
     */
    public function setContainer(ContainerInterface $container = null)
    {
        dump('Here in the ContainerAwareTrait');
        dump(null === $container);
        $this->container = $container;
    }
}

This dumps

Here in the ContainerAwareTrait
false

So the autowiring works well and sets the container.

But in the ControllerTrait I have this:

trait ControllerTrait
{
    /**
     * Generates a URL from the given parameters.
     *
     * @param string $route         The name of the route
     * @param mixed  $parameters    An array of parameters
     * @param int    $referenceType The type of reference (one of the constants in UrlGeneratorInterface)
     *
     * @return string The generated URL
     *
     * @see UrlGeneratorInterface
     */
    protected function generateUrl($route, $parameters = array(), $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH)
    {
        dump('Here in the ControllerTrait');
        die(dump(null === $this->container));
        return $this->container->get('router')->generate($route, $parameters, $referenceType);
    }

    ...

this is the dump:

Here in the ControllerTrait
true

So here the container is null and this causes the error.

Anyone can help me solve this issue?

Why is the container null?

If may help, this is the services.yml configuration (the default that cames with Symfony):

# controllers are imported separately to make sure they're public
# and have a tag that allows actions to type-hint services
AppBundle\Controller\:
    resource: '../../src/AppBundle/Controller'
    public: true
    tags: ['controller.service_arguments']

This issue is a copy and paste of a Stackoverflow question I opened.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions