Skip to content

[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

Closed
wants to merge 2 commits into from

Conversation

rjwebdev
Copy link
Contributor

@rjwebdev rjwebdev commented May 2, 2019

Q A
Branch? 3.4
Bug fix? yes
New feature? no
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets #31271
License MIT

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.

@rjwebdev
Copy link
Contributor Author

rjwebdev commented May 3, 2019

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]);
Copy link
Contributor

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());

@xabbuh xabbuh added this to the 3.4 milestone May 4, 2019
@@ -24,6 +24,24 @@

class ControllerResolverTest extends ContainerControllerResolverTest
{
public function testGetControllerWithServiceWithoutMethod()
{
class_exists(AbstractControllerTest::class);
Copy link
Member

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?

Copy link
Contributor Author

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

@nicolas-grekas
Copy link
Member

That's a new feature, can you rebase+retarget master please?

@rjwebdev rjwebdev changed the base branch from 3.4 to master May 6, 2019 06:33
@rjwebdev rjwebdev force-pushed the controller-invoke branch from 66d1c8e to 881a02f Compare May 6, 2019 06:36
@HypeMC
Copy link
Contributor

HypeMC commented May 6, 2019

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.

@nicolas-grekas
Copy link
Member

So fixed in #26085, let's close?

@Tobion
Copy link
Contributor

Tobion commented May 6, 2019

Closing as this is a new feature that has been implemented for consistency in #26085

@Tobion Tobion closed this May 6, 2019
@Tobion
Copy link
Contributor

Tobion commented May 6, 2019

Beware that auto-injection of the container is also deprecated in #27462. So you define it as a service subscriber, e.g. by setting autoconfigure: true

@rjwebdev rjwebdev deleted the controller-invoke branch May 6, 2019 17:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
7 participants