diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/fragment_renderer.php b/src/Symfony/Bundle/FrameworkBundle/Resources/config/fragment_renderer.php index 2d42a10026f05..80d7e2cb157b1 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/fragment_renderer.php +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/fragment_renderer.php @@ -13,6 +13,8 @@ use Symfony\Component\HttpKernel\DependencyInjection\LazyLoadingFragmentHandler; use Symfony\Component\HttpKernel\Fragment\EsiFragmentRenderer; +use Symfony\Component\HttpKernel\Fragment\FragmentUriGenerator; +use Symfony\Component\HttpKernel\Fragment\FragmentUriGeneratorInterface; use Symfony\Component\HttpKernel\Fragment\HIncludeFragmentRenderer; use Symfony\Component\HttpKernel\Fragment\InlineFragmentRenderer; use Symfony\Component\HttpKernel\Fragment\SsiFragmentRenderer; @@ -31,6 +33,10 @@ param('kernel.debug'), ]) + ->set('fragment.uri_generator', FragmentUriGenerator::class) + ->args([param('fragment.path'), service('uri_signer')]) + ->alias(FragmentUriGeneratorInterface::class, 'fragment.uri_generator') + ->set('fragment.renderer.inline', InlineFragmentRenderer::class) ->args([service('http_kernel'), service('event_dispatcher')]) ->call('setFragmentPath', [param('fragment.path')]) diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php index 9d5d1aa9fb544..7eb28b9d123db 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php @@ -49,6 +49,7 @@ use Symfony\Component\HttpClient\ScopingHttpClient; use Symfony\Component\HttpFoundation\Session\SessionInterface; use Symfony\Component\HttpKernel\DependencyInjection\LoggerPass; +use Symfony\Component\HttpKernel\Fragment\FragmentUriGeneratorInterface; use Symfony\Component\Messenger\Transport\TransportFactory; use Symfony\Component\PropertyAccess\PropertyAccessor; use Symfony\Component\Security\Core\Security; @@ -181,6 +182,8 @@ public function testEsiDisabled() public function testFragmentsAndHinclude() { $container = $this->createContainerFromFile('fragments_and_hinclude'); + $this->assertTrue($container->has('fragment.uri_generator')); + $this->assertTrue($container->hasAlias(FragmentUriGeneratorInterface::class)); $this->assertTrue($container->hasParameter('fragment.renderer.hinclude.global_template')); $this->assertEquals('global_hinclude_template', $container->getParameter('fragment.renderer.hinclude.global_template')); } diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/Bundle/TestBundle/Controller/FragmentController.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/Bundle/TestBundle/Controller/FragmentController.php index 8436a3b24809a..42044570201e9 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/Bundle/TestBundle/Controller/FragmentController.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/Bundle/TestBundle/Controller/FragmentController.php @@ -15,6 +15,8 @@ use Symfony\Component\DependencyInjection\ContainerAwareTrait; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\HttpKernel\Controller\ControllerReference; +use Symfony\Component\HttpKernel\Fragment\FragmentUriGeneratorInterface; use Twig\Environment; class FragmentController implements ContainerAwareInterface @@ -45,6 +47,11 @@ public function forwardLocaleAction(Request $request) { return new Response($request->getLocale()); } + + public function fragmentUriAction(Request $request, FragmentUriGeneratorInterface $fragmentUriGenerator) + { + return new Response($fragmentUriGenerator->generate(new ControllerReference(self::class.'::indexAction'), $request)); + } } class Bar diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/Bundle/TestBundle/Resources/config/routing.yml b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/Bundle/TestBundle/Resources/config/routing.yml index 94eb4d7ac1293..d790c1a13125b 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/Bundle/TestBundle/Resources/config/routing.yml +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/Bundle/TestBundle/Resources/config/routing.yml @@ -57,6 +57,10 @@ fragment_inlined: path: /fragment_inlined defaults: { _controller: Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Controller\FragmentController::inlinedAction } +fragment_uri: + path: /fragment_uri + defaults: { _controller: Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Controller\FragmentController::fragmentUriAction } + array_controller: path: /array_controller defaults: { _controller: [ArrayController, someAction] } diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/FragmentTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/FragmentTest.php index a4ac17238a4b8..b514ed3b8e042 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/FragmentTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/FragmentTest.php @@ -44,4 +44,12 @@ public function getConfigs() [true], ]; } + + public function testGenerateFragmentUri() + { + $client = self::createClient(['test_case' => 'Fragment', 'root_config' => 'config.yml', 'debug' => true]); + $client->request('GET', '/fragment_uri'); + + $this->assertSame('/_fragment?_hash=CCRGN2D%2FoAJbeGz%2F%2FdoH3bNSPwLCrmwC1zAYCGIKJ0E%3D&_path=_format%3Dhtml%26_locale%3Den%26_controller%3DSymfony%255CBundle%255CFrameworkBundle%255CTests%255CFunctional%255CBundle%255CTestBundle%255CController%255CFragmentController%253A%253AindexAction', $client->getResponse()->getContent()); + } } diff --git a/src/Symfony/Component/HttpKernel/Fragment/FragmentUriGenerator.php b/src/Symfony/Component/HttpKernel/Fragment/FragmentUriGenerator.php index 5a62b9b5bfae6..bf6201e14904a 100644 --- a/src/Symfony/Component/HttpKernel/Fragment/FragmentUriGenerator.php +++ b/src/Symfony/Component/HttpKernel/Fragment/FragmentUriGenerator.php @@ -12,6 +12,7 @@ namespace Symfony\Component\HttpKernel\Fragment; use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpKernel\Controller\ControllerReference; use Symfony\Component\HttpKernel\UriSigner; /** @@ -34,7 +35,7 @@ public function __construct(string $fragmentPath, UriSigner $signer = null) /** * {@inheritDoc} */ - public function generate($controller, Request $request, bool $absolute = false, bool $strict = true, bool $sign = true): string + public function generate(ControllerReference $controller, Request $request, bool $absolute = false, bool $strict = true, bool $sign = true): string { if ($sign && null === $this->signer) { throw new \LogicException('You must use a URI when using the ESI rendering strategy or set a URL signer.'); diff --git a/src/Symfony/Component/HttpKernel/Fragment/FragmentUriGeneratorInterface.php b/src/Symfony/Component/HttpKernel/Fragment/FragmentUriGeneratorInterface.php index f25ad9005da84..1b615f343243d 100644 --- a/src/Symfony/Component/HttpKernel/Fragment/FragmentUriGeneratorInterface.php +++ b/src/Symfony/Component/HttpKernel/Fragment/FragmentUriGeneratorInterface.php @@ -24,11 +24,11 @@ interface FragmentUriGeneratorInterface /** * Generates a fragment URI for a given controller. * - * @param string|ControllerReference $controller The name of a controller as a string or a ControllerReference instance - * @param bool $absolute Whether to generate an absolute URL or not - * @param bool $strict Whether to allow non-scalar attributes or not + * @param ControllerReference $controller The name of a controller as a string or a ControllerReference instance + * @param bool $absolute Whether to generate an absolute URL or not + * @param bool $strict Whether to allow non-scalar attributes or not * * @return string A fragment URI */ - public function generate($controller, Request $request, bool $absolute = false, bool $strict = true, bool $sign = true): string; + public function generate(ControllerReference $controller, Request $request, bool $absolute = false, bool $strict = true, bool $sign = true): string; }