Skip to content

Commit 699e256

Browse files
committed
[LiveComponent] Fix LiveUrl by replacing context when matching route
1 parent 337b860 commit 699e256

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

src/LiveComponent/src/Util/UrlFactory.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
use Symfony\Component\Routing\Exception\MethodNotAllowedException;
1515
use Symfony\Component\Routing\Exception\MissingMandatoryParametersException;
1616
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
17+
use Symfony\Component\Routing\Matcher\UrlMatcher;
18+
use Symfony\Component\Routing\Matcher\UrlMatcherInterface;
1719
use Symfony\Component\Routing\RouterInterface;
1820

1921
/**
@@ -60,10 +62,24 @@ public function createFromPreviousAndProps(
6062

6163
private function createPath(string $previousUrl, array $props): string
6264
{
63-
return $this->router->generate(
64-
$this->router->match($previousUrl)['_route'] ?? '',
65+
$newPath = $this->router->generate(
66+
$this->matchRoute($previousUrl),
6567
$props
6668
);
69+
70+
return $newPath;
71+
}
72+
73+
private function matchRoute(string $previousUrl): string
74+
{
75+
$context = $this->router->getContext();
76+
$tmpContext = clone $context;
77+
$tmpContext->setMethod('GET');
78+
$this->router->setContext($tmpContext);
79+
$match = $this->router->match($previousUrl);
80+
$this->router->setContext($context);
81+
82+
return $match['_route'] ?? '';
6783
}
6884

6985
private function replaceQueryString($url, array $props): string

src/LiveComponent/tests/Unit/Util/UrlFactoryTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\Routing\Exception\MethodNotAllowedException;
1616
use Symfony\Component\Routing\Exception\MissingMandatoryParametersException;
1717
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
18+
use Symfony\Component\Routing\RequestContext;
1819
use Symfony\Component\Routing\RouterInterface;
1920
use Symfony\UX\LiveComponent\Util\UrlFactory;
2021

@@ -178,7 +179,13 @@ private function createRouterStub(
178179
array $props = [],
179180
): RouterInterface {
180181
$matchedRoute = 'default';
182+
$context = $this->createMock(RequestContext::class);
181183
$router = $this->createMock(RouterInterface::class);
184+
$router->expects(self::once())
185+
->method('getContext')
186+
->willReturn($context);
187+
$router->expects(self::exactly(2))
188+
->method('setContext');
182189
$router->expects(self::once())
183190
->method('match')
184191
->with($previousUrl)

0 commit comments

Comments
 (0)