Skip to content

Commit 7dbf0d1

Browse files
committed
fix: [BC] feature testing may use incorrect HTTP verb for auto routing improved
Fixes AutoRouterInterface::getRoute().
1 parent d4bcffd commit 7dbf0d1

4 files changed

Lines changed: 17 additions & 12 deletions

File tree

system/Router/AutoRouter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public function __construct(
8080
*
8181
* @return array [directory_name, controller_name, controller_method, params]
8282
*/
83-
public function getRoute(string $uri): array
83+
public function getRoute(string $uri, string $httpVerb): array
8484
{
8585
$segments = explode('/', $uri);
8686

system/Router/AutoRouterImproved.php

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,10 @@ final class AutoRouterImproved implements AutoRouterInterface
6060

6161
/**
6262
* HTTP verb for the request.
63+
*
64+
* @deprecated No longer used.
6365
*/
64-
private string $httpVerb;
66+
private string $httpVerb; // @phpstan-ignore-line
6567

6668
/**
6769
* The namespace for controllers.
@@ -74,15 +76,17 @@ final class AutoRouterImproved implements AutoRouterInterface
7476
private string $defaultController;
7577

7678
/**
77-
* The name of the default method
79+
* The name of the default method without HTTP verb prefix.
7880
*/
7981
private string $defaultMethod;
8082

8183
/**
8284
* @param class-string[] $protectedControllers
8385
* @param string $defaultController Short classname
86+
*
87+
* @deprecated $httpVerb is deprecated. No longer used.
8488
*/
85-
public function __construct(
89+
public function __construct(// @phpstan-ignore-line
8690
array $protectedControllers,
8791
string $namespace,
8892
string $defaultController,
@@ -93,22 +97,23 @@ public function __construct(
9397
$this->protectedControllers = $protectedControllers;
9498
$this->namespace = rtrim($namespace, '\\') . '\\';
9599
$this->translateURIDashes = $translateURIDashes;
96-
$this->httpVerb = $httpVerb;
97100
$this->defaultController = $defaultController;
98-
$this->defaultMethod = $httpVerb . ucfirst($defaultMethod);
101+
$this->defaultMethod = $defaultMethod;
99102

100103
// Set the default values
101104
$this->controller = $this->defaultController;
102-
$this->method = $this->defaultMethod;
103105
}
104106

105107
/**
106108
* Finds controller, method and params from the URI.
107109
*
108110
* @return array [directory_name, controller_name, controller_method, params]
109111
*/
110-
public function getRoute(string $uri): array
112+
public function getRoute(string $uri, string $httpVerb): array
111113
{
114+
$defaultMethod = $httpVerb . ucfirst($this->defaultMethod);
115+
$this->method = $defaultMethod;
116+
112117
$segments = explode('/', $uri);
113118

114119
// WARNING: Directories get shifted out of the segments array.
@@ -144,10 +149,10 @@ public function getRoute(string $uri): array
144149
$methodSegment = $this->translateURIDashes(array_shift($nonDirSegments));
145150

146151
// Prefix HTTP verb
147-
$this->method = $this->httpVerb . ucfirst($methodSegment);
152+
$this->method = $httpVerb . ucfirst($methodSegment);
148153

149154
// Prevent access to default method path
150-
if (strtolower($this->method) === strtolower($this->defaultMethod)) {
155+
if (strtolower($this->method) === strtolower($defaultMethod)) {
151156
throw new PageNotFoundException(
152157
'Cannot access the default method "' . $this->method . '" with the method name URI path.'
153158
);

system/Router/AutoRouterInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,5 @@ interface AutoRouterInterface
2121
*
2222
* @return array [directory_name, controller_name, controller_method, params]
2323
*/
24-
public function getRoute(string $uri): array;
24+
public function getRoute(string $uri, string $httpVerb): array;
2525
}

system/Router/Router.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@ protected function checkRoutes(string $uri): bool
504504
public function autoRoute(string $uri)
505505
{
506506
[$this->directory, $this->controller, $this->method, $this->params]
507-
= $this->autoRouter->getRoute($uri);
507+
= $this->autoRouter->getRoute($uri, $this->collection->getHTTPVerb());
508508
}
509509

510510
/**

0 commit comments

Comments
 (0)