-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[FrameworkBundle]: use __invoke function if no method is defined in r… #31367
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
8bf1b49
to
66d1c8e
Compare
Ok, I added some piece of code so that the __invoke method of the controller will be called when no method is found. But unfortunately the tests fail when the __constructor should be called because of my code change. I'm not sure on how to fix this. Should I check if the controller is used in the routing context or not to fix this or does anyone have another idea? |
$controller = $resolver->getController($request); | ||
|
||
$this->assertSame($container, $controller[0]->getContainer()); | ||
$this->assertSame('__invoke', $controller[1]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rjwebdev I would follow the same logic as in testGetControllerOnContainerAwareInvokable
:
$this->assertInstanceOf('Symfony\Bundle\FrameworkBundle\Tests\Controller\DummyController', $controller);
$this->assertInstanceOf('Symfony\Component\DependencyInjection\ContainerInterface', $controller->getContainer());
@@ -24,6 +24,24 @@ | |||
|
|||
class ControllerResolverTest extends ContainerControllerResolverTest | |||
{ | |||
public function testGetControllerWithServiceWithoutMethod() | |||
{ | |||
class_exists(AbstractControllerTest::class); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the reason for the class_exists()
call here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No specific reason. Think it's better to remove it but since I have to start from master branch, I should rework the entire new code
That's a new feature, can you rebase+retarget master please? |
66d1c8e
to
881a02f
Compare
Just a note, this bug is not present on the master branch, it was fixed starting with 4.1 with the various changes done to the controller resolves in f8a609c, therefore this change is not required. The test case would be nice though. |
So fixed in #26085, let's close? |
Closing as this is a new feature that has been implemented for consistency in #26085 |
Beware that auto-injection of the container is also deprecated in #27462. So you define it as a service subscriber, e.g. by setting |
Fixes #31271
When a service id for a controller is used for the controller configuration of a routing and no method is defined, there's no container injected in this controller.
With this fix, the __invoke method of the controller will be set as the method to call for the given route in the ControllerResolver.
A small unit test was added to check the new code.