Skip to content

Commit 74b213d

Browse files
committed
feature #40575 [FrameworkBundle][HttpKernel][TwigBridge] Add an helper to generate fragments URL (dunglas)
This PR was squashed before being merged into the 5.3-dev branch. Discussion ---------- [FrameworkBundle][HttpKernel][TwigBridge] Add an helper to generate fragments URL | Q | A | ------------- | --- | Branch? | 5.x | Bug fix? | no | New feature? | yes <!-- please update src/**/CHANGELOG.md files --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tickets | Fix n/a | License | MIT | Doc PR | todo This PR adds a new helper to generate raw fragment URL. Fragments will be useful to generate lazy frames with Symfony UX Turbo (symfony/ux#64). This will also be convenient when using hinclude, ESI etc in case you want full control over the generated HTML. This is also more in sync with the new best practices we apply in the form component (generate the HTML by yourself instead of using Twig helpers hiding the HTML elements). Example: ```html <turbo-frame id="set_aside_tray" src="{{ fragment_uri(controller('Symfony\Bundle\FrameworkBundle\Controller', {template: "foo.html.twig"})) }}"> <img src="/icons/spinner.gif"> </turbo-frame> ``` Commits ------- 5d29d76612 [FrameworkBundle][HttpKernel][TwigBridge] Add an helper to generate fragments URL
2 parents eee9307 + 345a893 commit 74b213d

File tree

6 files changed

+29
-0
lines changed

6 files changed

+29
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ CHANGELOG
1818
* Deprecate all other values than "none", "php_array" and "file" for `framework.annotation.cache`
1919
* Add `KernelTestCase::getContainer()` as the best way to get a container in tests
2020
* Rename the container parameter `profiler_listener.only_master_requests` to `profiler_listener.only_main_requests`
21+
* Add service `fragment.uri_generator` to generate the URI of a fragment
2122

2223
5.2.0
2324
-----

Resources/config/fragment_renderer.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
use Symfony\Component\HttpKernel\DependencyInjection\LazyLoadingFragmentHandler;
1515
use Symfony\Component\HttpKernel\Fragment\EsiFragmentRenderer;
16+
use Symfony\Component\HttpKernel\Fragment\FragmentUriGenerator;
17+
use Symfony\Component\HttpKernel\Fragment\FragmentUriGeneratorInterface;
1618
use Symfony\Component\HttpKernel\Fragment\HIncludeFragmentRenderer;
1719
use Symfony\Component\HttpKernel\Fragment\InlineFragmentRenderer;
1820
use Symfony\Component\HttpKernel\Fragment\SsiFragmentRenderer;
@@ -31,6 +33,10 @@
3133
param('kernel.debug'),
3234
])
3335

36+
->set('fragment.uri_generator', FragmentUriGenerator::class)
37+
->args([param('fragment.path'), service('uri_signer')])
38+
->alias(FragmentUriGeneratorInterface::class, 'fragment.uri_generator')
39+
3440
->set('fragment.renderer.inline', InlineFragmentRenderer::class)
3541
->args([service('http_kernel'), service('event_dispatcher')])
3642
->call('setFragmentPath', [param('fragment.path')])

Tests/DependencyInjection/FrameworkExtensionTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
use Symfony\Component\HttpClient\ScopingHttpClient;
5151
use Symfony\Component\HttpFoundation\Session\SessionInterface;
5252
use Symfony\Component\HttpKernel\DependencyInjection\LoggerPass;
53+
use Symfony\Component\HttpKernel\Fragment\FragmentUriGeneratorInterface;
5354
use Symfony\Component\Messenger\Transport\TransportFactory;
5455
use Symfony\Component\PropertyAccess\PropertyAccessor;
5556
use Symfony\Component\Security\Core\Security;
@@ -182,6 +183,8 @@ public function testEsiDisabled()
182183
public function testFragmentsAndHinclude()
183184
{
184185
$container = $this->createContainerFromFile('fragments_and_hinclude');
186+
$this->assertTrue($container->has('fragment.uri_generator'));
187+
$this->assertTrue($container->hasAlias(FragmentUriGeneratorInterface::class));
185188
$this->assertTrue($container->hasParameter('fragment.renderer.hinclude.global_template'));
186189
$this->assertEquals('global_hinclude_template', $container->getParameter('fragment.renderer.hinclude.global_template'));
187190
}

Tests/Functional/Bundle/TestBundle/Controller/FragmentController.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
1616
use Symfony\Component\HttpFoundation\Request;
1717
use Symfony\Component\HttpFoundation\Response;
18+
use Symfony\Component\HttpKernel\Controller\ControllerReference;
19+
use Symfony\Component\HttpKernel\Fragment\FragmentUriGeneratorInterface;
1820
use Twig\Environment;
1921

2022
class FragmentController implements ContainerAwareInterface
@@ -45,6 +47,11 @@ public function forwardLocaleAction(Request $request)
4547
{
4648
return new Response($request->getLocale());
4749
}
50+
51+
public function fragmentUriAction(Request $request, FragmentUriGeneratorInterface $fragmentUriGenerator)
52+
{
53+
return new Response($fragmentUriGenerator->generate(new ControllerReference(self::class.'::indexAction'), $request));
54+
}
4855
}
4956

5057
class Bar

Tests/Functional/Bundle/TestBundle/Resources/config/routing.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ fragment_inlined:
5757
path: /fragment_inlined
5858
defaults: { _controller: Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Controller\FragmentController::inlinedAction }
5959

60+
fragment_uri:
61+
path: /fragment_uri
62+
defaults: { _controller: Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Controller\FragmentController::fragmentUriAction }
63+
6064
array_controller:
6165
path: /array_controller
6266
defaults: { _controller: [ArrayController, someAction] }

Tests/Functional/FragmentTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,12 @@ public function getConfigs()
4444
[true],
4545
];
4646
}
47+
48+
public function testGenerateFragmentUri()
49+
{
50+
$client = self::createClient(['test_case' => 'Fragment', 'root_config' => 'config.yml', 'debug' => true]);
51+
$client->request('GET', '/fragment_uri');
52+
53+
$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());
54+
}
4755
}

0 commit comments

Comments
 (0)