Skip to content

Commit e81aad2

Browse files
committed
Force RequestsContext to "GET"
1 parent eba1291 commit e81aad2

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

src/LiveComponent/src/Util/UrlFactory.php

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\Routing\Exception\MissingMandatoryParametersException;
1515
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
16+
use Symfony\Component\Routing\RequestContext;
1617
use Symfony\Component\Routing\RouterInterface;
1718

1819
/**
@@ -59,10 +60,22 @@ public function createFromPreviousAndProps(
5960

6061
private function createPath(string $previousUrl, array $props): string
6162
{
62-
return $this->router->generate(
63-
$this->router->match($previousUrl)['_route'] ?? '',
64-
$props
65-
);
63+
// LiveComponent uses POST requests by default but previousUrl might only match a GET route.
64+
$ret = $this->router->getContext();
65+
$this->router->setContext(RequestContext::fromUri($previousUrl));
66+
try {
67+
$route = $this->router->match($previousUrl);
68+
} catch (\Exception $e) {
69+
return $previousUrl;
70+
} finally {
71+
$this->router->setContext($ret);
72+
}
73+
74+
if (!isset($route['_route']) || '' === $route['_route']) {
75+
return $previousUrl;
76+
}
77+
78+
return $this->router->generate($route['_route'] ?? '', $props);
6679
}
6780

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

0 commit comments

Comments
 (0)