Skip to content

Commit

Permalink
Merge pull request #8235 from kenjis/fix-route-HTTP-verb
Browse files Browse the repository at this point in the history
fix: route key lowercase HTTP verbs
  • Loading branch information
kenjis authored Nov 26, 2023
2 parents 21e117b + 72f16ab commit 59fa588
Show file tree
Hide file tree
Showing 22 changed files with 232 additions and 163 deletions.
2 changes: 1 addition & 1 deletion system/CodeIgniter.php
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ public function disableFilters(): void
*/
protected function handleRequest(?RouteCollectionInterface $routes, Cache $cacheConfig, bool $returnResponse = false)
{
if ($this->request instanceof IncomingRequest && strtolower($this->request->getMethod()) === 'cli') {
if ($this->request instanceof IncomingRequest && $this->request->getMethod() === 'CLI') {
return $this->response->setStatusCode(405)->setBody('Method Not Allowed');
}

Expand Down
8 changes: 4 additions & 4 deletions system/Commands/Utilities/FilterCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class FilterCheck extends BaseCommand
* @var array<string, string>
*/
protected $arguments = [
'method' => 'The HTTP method. get, post, put, etc.',
'method' => 'The HTTP method. GET, POST, PUT, etc.',
'route' => 'The route (URI path) to check filters.',
];

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

return EXIT_ERROR;
}

$method = strtolower($params[0]);
$method = $params[0];
$route = $params[1];

// Load Routes
Expand Down
18 changes: 4 additions & 14 deletions system/Commands/Utilities/Routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use CodeIgniter\Commands\Utilities\Routes\FilterCollector;
use CodeIgniter\Commands\Utilities\Routes\SampleURIGenerator;
use CodeIgniter\Router\DefinedRouteCollector;
use CodeIgniter\Router\Router;
use Config\Feature;
use Config\Routing;
use Config\Services;
Expand Down Expand Up @@ -98,18 +99,7 @@ public function run(array $params)
unset($_SERVER['HTTP_HOST']);
}

$methods = [
'get',
'head',
'post',
'patch',
'put',
'delete',
'options',
'trace',
'connect',
'cli',
];
$methods = Router::HTTP_METHODS;

$tbody = [];
$uriGenerator = new SampleURIGenerator();
Expand Down Expand Up @@ -172,8 +162,8 @@ public function run(array $params)
$autoRoutes = $autoRouteCollector->get();

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

$routes[] = implode(' ', array_map('class_basename', $filters['before']));
$routes[] = implode(' ', array_map('class_basename', $filters['after']));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function read(string $class, string $defaultController = 'Home', string $
$methodName = $method->getName();

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

Expand Down
18 changes: 16 additions & 2 deletions system/Commands/Utilities/Routes/FilterCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,28 @@ public function __construct(bool $resetRoutes = false)
}

/**
* @param string $method HTTP method
* @param string $method HTTP verb like `GET`,`POST` or `CLI`.
* @param string $uri URI path to find filters for
*
* @return array{before: list<string>, after: list<string>} array of filter alias or classname
*/
public function get(string $method, string $uri): array
{
if ($method === 'cli') {
if ($method === strtolower($method)) {
@trigger_error(
'Passing lowercase HTTP method "' . $method . '" is deprecated.'
. ' Use uppercase HTTP method like "' . strtoupper($method) . '".',
E_USER_DEPRECATED
);
}

/**
* @deprecated 4.5.0
* @TODO Remove this in the future.
*/
$method = strtoupper($method);

if ($method === 'CLI') {
return [
'before' => [],
'after' => [],
Expand Down
1 change: 0 additions & 1 deletion system/HTTP/CLIRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,6 @@ public function getLocale(): string
* Checks this request type.
*
* @param string $type HTTP verb or 'json' or 'ajax'
* @phpstan-param string|'get'|'post'|'put'|'delete'|'head'|'patch'|'options'|'json'|'ajax' $type
*/
public function is(string $type): bool
{
Expand Down
1 change: 0 additions & 1 deletion system/HTTP/IncomingRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,6 @@ public function negotiate(string $type, array $supported, bool $strictMatch = fa
*
* @param string $type HTTP verb or 'json' or 'ajax'.
* HTTP verb should be case-sensitive, but this is case-insensitive.
* @phpstan-param string|'get'|'post'|'put'|'delete'|'head'|'patch'|'options'|'json'|'ajax' $type
*/
public function is(string $type): bool
{
Expand Down
2 changes: 1 addition & 1 deletion system/Router/AutoRouter.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public function getRoute(string $uri, string $httpVerb): array
}

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

$controller .= $this->directory ? str_replace('/', '\\', $this->directory) : '';
Expand Down
13 changes: 1 addition & 12 deletions system/Router/DefinedRouteCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,7 @@ public function __construct(RouteCollection $routes)
*/
public function collect(): Generator
{
$methods = [
'get',
'head',
'post',
'patch',
'put',
'delete',
'options',
'trace',
'connect',
'cli',
];
$methods = Router::HTTP_METHODS;

foreach ($methods as $method) {
$routes = $this->routeCollection->getRoutes($method);
Expand Down
Loading

0 comments on commit 59fa588

Please sign in to comment.