Skip to content

Commit 59fa588

Browse files
authored
Merge pull request #8235 from kenjis/fix-route-HTTP-verb
fix: route key lowercase HTTP verbs
2 parents 21e117b + 72f16ab commit 59fa588

22 files changed

+232
-163
lines changed

system/CodeIgniter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@ public function disableFilters(): void
453453
*/
454454
protected function handleRequest(?RouteCollectionInterface $routes, Cache $cacheConfig, bool $returnResponse = false)
455455
{
456-
if ($this->request instanceof IncomingRequest && strtolower($this->request->getMethod()) === 'cli') {
456+
if ($this->request instanceof IncomingRequest && $this->request->getMethod() === 'CLI') {
457457
return $this->response->setStatusCode(405)->setBody('Method Not Allowed');
458458
}
459459

system/Commands/Utilities/FilterCheck.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ class FilterCheck extends BaseCommand
5656
* @var array<string, string>
5757
*/
5858
protected $arguments = [
59-
'method' => 'The HTTP method. get, post, put, etc.',
59+
'method' => 'The HTTP method. GET, POST, PUT, etc.',
6060
'route' => 'The route (URI path) to check filters.',
6161
];
6262

@@ -76,13 +76,13 @@ public function run(array $params)
7676
if (! isset($params[0], $params[1])) {
7777
CLI::error('You must specify a HTTP verb and a route.');
7878
CLI::write(' Usage: ' . $this->usage);
79-
CLI::write('Example: filter:check get /');
80-
CLI::write(' filter:check put products/1');
79+
CLI::write('Example: filter:check GET /');
80+
CLI::write(' filter:check PUT products/1');
8181

8282
return EXIT_ERROR;
8383
}
8484

85-
$method = strtolower($params[0]);
85+
$method = $params[0];
8686
$route = $params[1];
8787

8888
// Load Routes

system/Commands/Utilities/Routes.php

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use CodeIgniter\Commands\Utilities\Routes\FilterCollector;
1919
use CodeIgniter\Commands\Utilities\Routes\SampleURIGenerator;
2020
use CodeIgniter\Router\DefinedRouteCollector;
21+
use CodeIgniter\Router\Router;
2122
use Config\Feature;
2223
use Config\Routing;
2324
use Config\Services;
@@ -98,18 +99,7 @@ public function run(array $params)
9899
unset($_SERVER['HTTP_HOST']);
99100
}
100101

101-
$methods = [
102-
'get',
103-
'head',
104-
'post',
105-
'patch',
106-
'put',
107-
'delete',
108-
'options',
109-
'trace',
110-
'connect',
111-
'cli',
112-
];
102+
$methods = Router::HTTP_METHODS;
113103

114104
$tbody = [];
115105
$uriGenerator = new SampleURIGenerator();
@@ -172,8 +162,8 @@ public function run(array $params)
172162
$autoRoutes = $autoRouteCollector->get();
173163

174164
foreach ($autoRoutes as &$routes) {
175-
// There is no `auto` method, but it is intentional not to get route filters.
176-
$filters = $filterCollector->get('auto', $uriGenerator->get($routes[1]));
165+
// There is no `AUTO` method, but it is intentional not to get route filters.
166+
$filters = $filterCollector->get('AUTO', $uriGenerator->get($routes[1]));
177167

178168
$routes[] = implode(' ', array_map('class_basename', $filters['before']));
179169
$routes[] = implode(' ', array_map('class_basename', $filters['after']));

system/Commands/Utilities/Routes/AutoRouterImproved/ControllerMethodReader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public function read(string $class, string $defaultController = 'Home', string $
6565
$methodName = $method->getName();
6666

6767
foreach ($this->httpMethods as $httpVerb) {
68-
if (strpos($methodName, $httpVerb) === 0) {
68+
if (strpos($methodName, strtolower($httpVerb)) === 0) {
6969
// Remove HTTP verb prefix.
7070
$methodInUri = lcfirst(substr($methodName, strlen($httpVerb)));
7171

system/Commands/Utilities/Routes/FilterCollector.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,28 @@ public function __construct(bool $resetRoutes = false)
3737
}
3838

3939
/**
40-
* @param string $method HTTP method
40+
* @param string $method HTTP verb like `GET`,`POST` or `CLI`.
4141
* @param string $uri URI path to find filters for
4242
*
4343
* @return array{before: list<string>, after: list<string>} array of filter alias or classname
4444
*/
4545
public function get(string $method, string $uri): array
4646
{
47-
if ($method === 'cli') {
47+
if ($method === strtolower($method)) {
48+
@trigger_error(
49+
'Passing lowercase HTTP method "' . $method . '" is deprecated.'
50+
. ' Use uppercase HTTP method like "' . strtoupper($method) . '".',
51+
E_USER_DEPRECATED
52+
);
53+
}
54+
55+
/**
56+
* @deprecated 4.5.0
57+
* @TODO Remove this in the future.
58+
*/
59+
$method = strtoupper($method);
60+
61+
if ($method === 'CLI') {
4862
return [
4963
'before' => [],
5064
'after' => [],

system/HTTP/CLIRequest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,6 @@ public function getLocale(): string
316316
* Checks this request type.
317317
*
318318
* @param string $type HTTP verb or 'json' or 'ajax'
319-
* @phpstan-param string|'get'|'post'|'put'|'delete'|'head'|'patch'|'options'|'json'|'ajax' $type
320319
*/
321320
public function is(string $type): bool
322321
{

system/HTTP/IncomingRequest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,6 @@ public function negotiate(string $type, array $supported, bool $strictMatch = fa
398398
*
399399
* @param string $type HTTP verb or 'json' or 'ajax'.
400400
* HTTP verb should be case-sensitive, but this is case-insensitive.
401-
* @phpstan-param string|'get'|'post'|'put'|'delete'|'head'|'patch'|'options'|'json'|'ajax' $type
402401
*/
403402
public function is(string $type): bool
404403
{

system/Router/AutoRouter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ public function getRoute(string $uri, string $httpVerb): array
120120
}
121121

122122
// Ensure routes registered via $routes->cli() are not accessible via web.
123-
if ($this->httpVerb !== 'cli') {
123+
if ($this->httpVerb !== 'CLI') {
124124
$controller = '\\' . $this->defaultNamespace;
125125

126126
$controller .= $this->directory ? str_replace('/', '\\', $this->directory) : '';

system/Router/DefinedRouteCollector.php

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,7 @@ public function __construct(RouteCollection $routes)
3333
*/
3434
public function collect(): Generator
3535
{
36-
$methods = [
37-
'get',
38-
'head',
39-
'post',
40-
'patch',
41-
'put',
42-
'delete',
43-
'options',
44-
'trace',
45-
'connect',
46-
'cli',
47-
];
36+
$methods = Router::HTTP_METHODS;
4837

4938
foreach ($methods as $method) {
5039
$routes = $this->routeCollection->getRoutes($method);

0 commit comments

Comments
 (0)