Skip to content

Commit 3c0d084

Browse files
committed
Add a way to generate absolute component URLs
1 parent c883096 commit 3c0d084

File tree

11 files changed

+71
-2
lines changed

11 files changed

+71
-2
lines changed

src/LiveComponent/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
- Add support for URL binding in `LiveProp`
66
- Allow multiple `LiveListener` attributes on a single method.
7+
- Allow multiple `LiveListener` attributes on a single method.
8+
- Add a new `urlReferenceType` parameter to `AsLiveComponent`, which allows to
9+
generate different type URL (e.g. absolute) for the component Ajax calls.
710

811
## 2.13.2
912

src/LiveComponent/doc/index.rst

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3153,6 +3153,26 @@ Then specify this new route on your component:
31533153
use DefaultActionTrait;
31543154
}
31553155
3156+
.. versionadded:: 2.14
3157+
3158+
The ``urlReferenceType`` option was added in LiveComponents 2.14.
3159+
3160+
You can also control the type of the generated URL:
3161+
3162+
.. code-block:: diff
3163+
3164+
// src/Components/RandomNumber.php
3165+
+ use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
3166+
use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;
3167+
use Symfony\UX\LiveComponent\DefaultActionTrait;
3168+
3169+
- #[AsLiveComponent]
3170+
+ #[AsLiveComponent(urlReferenceType: UrlGeneratorInterface::ABSOLUTE_URL)]
3171+
class RandomNumber
3172+
{
3173+
use DefaultActionTrait;
3174+
}
3175+
31563176
Add a Hook on LiveProp Update
31573177
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
31583178

src/LiveComponent/src/Attribute/AsLiveComponent.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\UX\LiveComponent\Attribute;
1313

14+
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
1415
use Symfony\UX\TwigComponent\Attribute\AsTwigComponent;
1516

1617
/**
@@ -33,6 +34,7 @@ final class AsLiveComponent extends AsTwigComponent
3334
* @param string $attributesVar The name of the special "attributes" variable in the template
3435
* @param bool $csrf Whether to enable CSRF protection (default: true)
3536
* @param string $route The route used to render the component & handle actions (default: ux_live_component)
37+
* @param int $urlReferenceType Which type of URL should be generated for the given route. Use the constants from UrlGeneratorInterface (default: absolute path, e.g. "/dir/file").
3638
*/
3739
public function __construct(
3840
string $name = null,
@@ -42,6 +44,7 @@ public function __construct(
4244
string $attributesVar = 'attributes',
4345
public bool $csrf = true,
4446
public string $route = 'ux_live_component',
47+
public int $urlReferenceType = UrlGeneratorInterface::ABSOLUTE_PATH,
4548
) {
4649
parent::__construct($name, $template, $exposePublicProps, $attributesVar);
4750
}
@@ -56,6 +59,7 @@ public function serviceConfig(): array
5659
'live' => true,
5760
'csrf' => $this->csrf,
5861
'route' => $this->route,
62+
'url_reference_type' => $this->urlReferenceType,
5963
]);
6064
}
6165

src/LiveComponent/src/Twig/LiveComponentRuntime.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,6 @@ public function getComponentUrl(string $name, array $props = []): string
4545

4646
$metadata = $this->factory->metadataFor($mounted->getName());
4747

48-
return $this->urlGenerator->generate($metadata->get('route'), $params);
48+
return $this->urlGenerator->generate($metadata->get('route'), $params, $metadata->get('url_reference_type'));
4949
}
5050
}

src/LiveComponent/src/Util/LiveControllerAttributesCreator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public function attributesForRendering(MountedComponent $mounted, ComponentMetad
6161
$attributesCollection = $this->attributeHelper->create();
6262
$attributesCollection->setLiveController($mounted->getName());
6363

64-
$url = $this->urlGenerator->generate($metadata->get('route'), ['_live_component' => $mounted->getName()]);
64+
$url = $this->urlGenerator->generate($metadata->get('route'), ['_live_component' => $mounted->getName()], $metadata->get('url_reference_type'));
6565
$attributesCollection->setUrl($url);
6666

6767
$liveListeners = AsLiveComponent::liveListeners($mounted->getComponent());
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Symfony\UX\LiveComponent\Tests\Fixtures\Component;
4+
5+
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
6+
use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;
7+
use Symfony\UX\LiveComponent\DefaultActionTrait;
8+
9+
/**
10+
* @author Gábor Egyed <[email protected]>
11+
*/
12+
#[AsLiveComponent('with_absolute_url', urlReferenceType: UrlGeneratorInterface::ABSOLUTE_URL)]
13+
final class WithAbsoluteUrl
14+
{
15+
use DefaultActionTrait;
16+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
{{ component_url('component1', {prop1: null, prop2: date}) }}
22
{{ component_url('alternate_route') }}
3+
{{ component_url('with_absolute_url') }}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<div{{ attributes }}>
2+
From absolute url.
3+
</div>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{{ component('with_absolute_url') }}

src/LiveComponent/tests/Functional/EventListener/AddLiveAttributesSubscriberTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,4 +154,24 @@ public function testQueryStringMappingAttribute()
154154

155155
$this->assertEquals($expected, $queryMapping);
156156
}
157+
158+
public function testAbsoluteUrl(): void
159+
{
160+
$div = $this->browser()
161+
->visit('/render-template/render_with_absolute_url')
162+
->assertSuccessful()
163+
->crawler()
164+
->filter('div')
165+
;
166+
167+
$props = json_decode($div->attr('data-live-props-value'), true);
168+
169+
$this->assertSame('live', $div->attr('data-controller'));
170+
$this->assertSame('http://localhost/_components/with_absolute_url', $div->attr('data-live-url-value'));
171+
$this->assertNotNull($div->attr('data-live-csrf-value'));
172+
$this->assertCount(2, $props);
173+
$this->assertArrayHasKey('@checksum', $props);
174+
$this->assertArrayHasKey('@attributes', $props);
175+
$this->assertArrayHasKey('data-live-id', $props['@attributes']);
176+
}
157177
}

0 commit comments

Comments
 (0)