Skip to content

Commit 1da8a7b

Browse files
committed
Cath MethodNotAllowedException
1 parent e81aad2 commit 1da8a7b

File tree

3 files changed

+20
-23
lines changed

3 files changed

+20
-23
lines changed

src/LiveComponent/src/EventListener/LiveUrlSubscriber.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,6 @@ public function onKernelResponse(ResponseEvent $event): void
4545
$newLiveUrl = null;
4646
if ($previousLiveUrl = $request->headers->get(self::URL_HEADER)) {
4747
$liveProps = $this->getLivePropsFromRequest($request);
48-
if ([] === $liveProps['path'] && [] === $liveProps['query']) {
49-
// If there are no live props, we don't need to change the URL
50-
return;
51-
}
52-
5348
$newLiveUrl = $this->urlFactory->createFromPreviousAndProps($previousLiveUrl, $liveProps['path'], $liveProps['query']);
5449
}
5550

src/LiveComponent/src/Util/UrlFactory.php

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111

1212
namespace Symfony\UX\LiveComponent\Util;
1313

14+
use Symfony\Component\Routing\Exception\MethodNotAllowedException;
1415
use Symfony\Component\Routing\Exception\MissingMandatoryParametersException;
1516
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
16-
use Symfony\Component\Routing\RequestContext;
1717
use Symfony\Component\Routing\RouterInterface;
1818

1919
/**
@@ -44,7 +44,7 @@ public function createFromPreviousAndProps(
4444

4545
try {
4646
$newUrl = $this->createPath($previousUrl, $pathMappedProps);
47-
} catch (ResourceNotFoundException|MissingMandatoryParametersException) {
47+
} catch (ResourceNotFoundException|MethodNotAllowedException|MissingMandatoryParametersException) {
4848
return null;
4949
}
5050

@@ -60,22 +60,10 @@ public function createFromPreviousAndProps(
6060

6161
private function createPath(string $previousUrl, array $props): string
6262
{
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);
63+
return $this->router->generate(
64+
$this->router->match($previousUrl)['_route'] ?? '',
65+
$props
66+
);
7967
}
8068

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

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\UX\LiveComponent\Tests\Unit\Util;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Routing\Exception\MethodNotAllowedException;
1516
use Symfony\Component\Routing\Exception\MissingMandatoryParametersException;
1617
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
1718
use Symfony\Component\Routing\RouterInterface;
@@ -140,6 +141,19 @@ public function testResourceNotFoundException()
140141
$this->assertNull($factory->createFromPreviousAndProps($previousUrl, [], []));
141142
}
142143

144+
public function testMethodNotAllowedException()
145+
{
146+
$previousUrl = '/foo/bar';
147+
$router = $this->createMock(RouterInterface::class);
148+
$router->expects(self::once())
149+
->method('match')
150+
->with($previousUrl)
151+
->willThrowException(new MethodNotAllowedException(['GET']));
152+
$factory = new UrlFactory($router);
153+
154+
$this->assertNull($factory->createFromPreviousAndProps($previousUrl, [], []));
155+
}
156+
143157
public function testMissingMandatoryParametersException()
144158
{
145159
$previousUrl = '/foo/bar';

0 commit comments

Comments
 (0)