Skip to content

Commit 112ed03

Browse files
committed
bug #249 [LiveComponent] fix component_url twig function (kbond)
This PR was merged into the 2.x branch. Discussion ---------- [LiveComponent] fix `component_url` twig function | Q | A | ------------- | --- | Bug fix? | yes | New feature? | yes (kind of) | Tickets | n/a | License | MIT Previously, it didn't work as intended. It required a component instance. It was supposed to work like `component()` (from twig-components), except generate a url to the live component (instead of rendering). Commits ------- 1ce27ec [LiveComponent] fix `component_url` twig function
2 parents e8e3721 + 1ce27ec commit 112ed03

File tree

3 files changed

+37
-3
lines changed

3 files changed

+37
-3
lines changed

src/LiveComponent/src/Twig/LiveComponentRuntime.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,10 @@ public function renderLiveAttributes(Environment $env, object $component, string
5959
);
6060
}
6161

62-
public function getComponentUrl(object $component, string $name = null): string
62+
public function getComponentUrl(string $name, array $props = []): string
6363
{
64-
$data = $this->hydrator->dehydrate($component);
65-
$params = ['component' => $this->nameFor($component, $name)] + $data;
64+
$component = $this->factory->create($name, $props);
65+
$params = ['component' => $name] + $this->hydrator->dehydrate($component);
6666

6767
return $this->urlGenerator->generate('live_component', $params);
6868
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{{ component_url('component1', { prop1: null, prop2: date }) }}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\UX\LiveComponent\Tests\Integration;
13+
14+
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
15+
use Symfony\UX\LiveComponent\Tests\ContainerBC;
16+
17+
/**
18+
* @author Kevin Bond <[email protected]>
19+
*/
20+
final class LiveComponentExtensionTest extends KernelTestCase
21+
{
22+
use ContainerBC;
23+
24+
public function testGetComponentUrl(): void
25+
{
26+
$rendered = self::getContainer()->get('twig')->render('component_url.html.twig', [
27+
'date' => new \DateTime('2022-10-06-0'),
28+
]);
29+
30+
$this->assertStringContainsString('/_components/component1?prop2=2022-10-06T00:00:00', $rendered);
31+
$this->assertStringContainsString('_checksum=', $rendered);
32+
}
33+
}

0 commit comments

Comments
 (0)