Skip to content

Commit 327515b

Browse files
committed
UrlMapping.mapPath
1 parent 0bc01cd commit 327515b

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

src/LiveComponent/src/EventListener/LiveUrlSubscriber.php

+15-5
Original file line numberDiff line numberDiff line change
@@ -67,19 +67,23 @@ private function getLivePropsToMap(Request $request): array
6767
$liveData = $request->attributes->get('_live_request_data') ?? [];
6868
$values = array_merge($liveData['props'] ?? [], $liveData['updated'] ?? []);
6969

70-
$urlLiveProps = [];
70+
$urlLiveProps = [
71+
'path' => [],
72+
'query' => []
73+
];
7174
foreach ($metadata->getAllLivePropsMetadata($component) as $liveProp) {
7275
$name = $liveProp->getName();
7376
$urlMapping = $liveProp->urlMapping();
7477
if (isset($values[$name]) && $urlMapping) {
75-
$urlLiveProps[$urlMapping->as ?? $name] = $values[$name];
78+
$urlLiveProps[$urlMapping->mapPath ? 'path' : 'query'][$urlMapping->as ?? $name] =
79+
$values[$name];
7680
}
7781
}
7882

7983
return $urlLiveProps;
8084
}
8185

82-
private function computeNewUrl(string $previousUrl, array $newProps): string
86+
private function computeNewUrl(string $previousUrl, array $livePropsToMap): string
8387
{
8488
$parsed = parse_url($previousUrl);
8589

@@ -89,10 +93,16 @@ private function computeNewUrl(string $previousUrl, array $newProps): string
8993
}
9094
parse_str($parsed['query'] ?? '', $previousQueryParams);
9195

92-
return $this->router->generate(
96+
$newUrl = $this->router->generate(
9397
$this->router->match($url)['_route'],
94-
array_merge($previousQueryParams, $newProps)
98+
array_merge($previousQueryParams, $livePropsToMap['path'])
9599
);
100+
parse_str(parse_url($newUrl)['query'] ?? '', $queryParams);
101+
$queryString = http_build_query(array_merge($queryParams, $livePropsToMap['query']));
102+
103+
return preg_replace('/[?#].*/', '', $newUrl) .
104+
('' !== $queryString ? '?' : '') .
105+
$queryString;
96106
}
97107

98108
/**

src/LiveComponent/src/Metadata/UrlMapping.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
namespace Symfony\UX\LiveComponent\Metadata;
1313

1414
/**
15-
* Mapping configuration to bind a LiveProp to a URL query parameter.
15+
* Mapping configuration to bind a LiveProp to a URL path or query parameter.
1616
*
1717
* @author Nicolas Rigaud <[email protected]>
1818
*/
@@ -23,6 +23,11 @@ public function __construct(
2323
* The name of the prop that appears in the URL. If null, the LiveProp's field name is used.
2424
*/
2525
public readonly ?string $as = null,
26+
27+
/**
28+
* True if the prop should be mapped to the path if it matches one of its parameters. Otherwise a query parameter will be used.
29+
*/
30+
public readonly bool $mapPath = false,
2631
) {
2732
}
2833
}

0 commit comments

Comments
 (0)