bug #15737 Fix the injection of the container in invokable controllers (stof)
This PR was merged into the 2.7 branch.
Discussion
----------
Fix the injection of the container in invokable controllers
| Q | A
| ------------- | ---
| Bug fix? | yes
| New feature? | no
| BC breaks? | no
| Deprecations? | no
| Tests pass? | yes
| Fixed tickets | -
| License | MIT
| Doc PR | n/a
https://github.com/symfony/symfony/pull/12022 added the necessary extension point in the ControllerResolver to hook into the instantiation (in 2.6+), but has not updated FrameworkBundle to use it.
I also reused the parent ``createController`` method instead of duplicating its code
Commits
-------
36e09da
Fix the injection of the container in invokable controllers
This commit is contained in:
commit
ac822f9c9b
@ -70,17 +70,20 @@ class ControllerResolver extends BaseControllerResolver
|
||||
}
|
||||
}
|
||||
|
||||
list($class, $method) = explode('::', $controller, 2);
|
||||
return parent::createController($controller);
|
||||
}
|
||||
|
||||
if (!class_exists($class)) {
|
||||
throw new \InvalidArgumentException(sprintf('Class "%s" does not exist.', $class));
|
||||
}
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function instantiateController($class)
|
||||
{
|
||||
$controller = parent::instantiateController($class);
|
||||
|
||||
$controller = $this->instantiateController($class);
|
||||
if ($controller instanceof ContainerAwareInterface) {
|
||||
$controller->setContainer($this->container);
|
||||
}
|
||||
|
||||
return array($controller, $method);
|
||||
return $controller;
|
||||
}
|
||||
}
|
||||
|
@ -33,6 +33,18 @@ class ControllerResolverTest extends BaseControllerResolverTest
|
||||
$this->assertSame('testAction', $controller[1]);
|
||||
}
|
||||
|
||||
public function testGetControllerOnContainerAwareInvokable()
|
||||
{
|
||||
$resolver = $this->createControllerResolver();
|
||||
$request = Request::create('/');
|
||||
$request->attributes->set('_controller', 'Symfony\Bundle\FrameworkBundle\Tests\Controller\ContainerAwareController');
|
||||
|
||||
$controller = $resolver->getController($request);
|
||||
|
||||
$this->assertInstanceOf('Symfony\Bundle\FrameworkBundle\Tests\Controller\ContainerAwareController', $controller);
|
||||
$this->assertInstanceOf('Symfony\Component\DependencyInjection\ContainerInterface', $controller->getContainer());
|
||||
}
|
||||
|
||||
public function testGetControllerWithBundleNotation()
|
||||
{
|
||||
$shortName = 'FooBundle:Default:test';
|
||||
@ -161,4 +173,8 @@ class ContainerAwareController implements ContainerAwareInterface
|
||||
public function testAction()
|
||||
{
|
||||
}
|
||||
|
||||
public function __invoke()
|
||||
{
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user