Skip to content

Commit bc97a0e

Browse files
authored
Merge pull request #8317 from kenjis/refactor-HTTP-verb-in-routes
refactor: HTTP verbs in Router
2 parents c846112 + a18869b commit bc97a0e

File tree

4 files changed

+44
-52
lines changed

4 files changed

+44
-52
lines changed

system/Router/AutoRouter.php

+4-9
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,6 @@ final class AutoRouter implements AutoRouterInterface
5050
*/
5151
private bool $translateURIDashes;
5252

53-
/**
54-
* HTTP verb for the request.
55-
*/
56-
private string $httpVerb;
57-
5853
/**
5954
* Default namespace for controllers.
6055
*/
@@ -65,13 +60,11 @@ public function __construct(
6560
string $defaultNamespace,
6661
string $defaultController,
6762
string $defaultMethod,
68-
bool $translateURIDashes,
69-
string $httpVerb
63+
bool $translateURIDashes
7064
) {
7165
$this->cliRoutes = $cliRoutes;
7266
$this->defaultNamespace = $defaultNamespace;
7367
$this->translateURIDashes = $translateURIDashes;
74-
$this->httpVerb = $httpVerb;
7568

7669
$this->controller = $defaultController;
7770
$this->method = $defaultMethod;
@@ -81,6 +74,8 @@ public function __construct(
8174
* Attempts to match a URI path against Controllers and directories
8275
* found in APPPATH/Controllers, to find a matching route.
8376
*
77+
* @param string $httpVerb HTTP verb like `GET`,`POST`
78+
*
8479
* @return array [directory_name, controller_name, controller_method, params]
8580
*/
8681
public function getRoute(string $uri, string $httpVerb): array
@@ -122,7 +117,7 @@ public function getRoute(string $uri, string $httpVerb): array
122117
}
123118

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

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

system/Router/AutoRouterImproved.php

+4-5
Original file line numberDiff line numberDiff line change
@@ -104,16 +104,13 @@ final class AutoRouterImproved implements AutoRouterInterface
104104
/**
105105
* @param class-string[] $protectedControllers
106106
* @param string $defaultController Short classname
107-
*
108-
* @deprecated $httpVerb is deprecated. No longer used.
109107
*/
110-
public function __construct(// @phpstan-ignore-line
108+
public function __construct(
111109
array $protectedControllers,
112110
string $namespace,
113111
string $defaultController,
114112
string $defaultMethod,
115-
bool $translateURIDashes,
116-
string $httpVerb
113+
bool $translateURIDashes
117114
) {
118115
$this->protectedControllers = $protectedControllers;
119116
$this->namespace = rtrim($namespace, '\\');
@@ -243,6 +240,8 @@ private function searchLastDefaultController(): bool
243240
/**
244241
* Finds controller, method and params from the URI.
245242
*
243+
* @param string $httpVerb HTTP verb like `GET`,`POST`
244+
*
246245
* @return array [directory_name, controller_name, controller_method, params]
247246
*/
248247
public function getRoute(string $uri, string $httpVerb): array

system/Router/Router.php

+2-4
Original file line numberDiff line numberDiff line change
@@ -151,17 +151,15 @@ public function __construct(RouteCollectionInterface $routes, ?Request $request
151151
$this->collection->getDefaultNamespace(),
152152
$this->collection->getDefaultController(),
153153
$this->collection->getDefaultMethod(),
154-
$this->translateURIDashes,
155-
$this->collection->getHTTPVerb()
154+
$this->translateURIDashes
156155
);
157156
} else {
158157
$this->autoRouter = new AutoRouter(
159158
$this->collection->getRoutes('CLI', false), // @phpstan-ignore-line
160159
$this->collection->getDefaultNamespace(),
161160
$this->collection->getDefaultController(),
162161
$this->collection->getDefaultMethod(),
163-
$this->translateURIDashes,
164-
$this->collection->getHTTPVerb()
162+
$this->translateURIDashes
165163
);
166164
}
167165
}

tests/system/Router/AutoRouterImprovedTest.php

+34-34
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use CodeIgniter\Config\Factories;
1717
use CodeIgniter\Config\Services;
1818
use CodeIgniter\Exceptions\PageNotFoundException;
19+
use CodeIgniter\HTTP\Method;
1920
use CodeIgniter\Router\Controllers\Dash_folder\Dash_controller;
2021
use CodeIgniter\Router\Controllers\Dash_folder\Home;
2122
use CodeIgniter\Router\Controllers\Index;
@@ -42,15 +43,14 @@ protected function setUp(): void
4243
$this->collection = new RouteCollection(Services::locator(), $moduleConfig, new Routing());
4344
}
4445

45-
private function createNewAutoRouter(string $httpVerb = 'get', $namespace = 'CodeIgniter\Router\Controllers'): AutoRouterImproved
46+
private function createNewAutoRouter($namespace = 'CodeIgniter\Router\Controllers'): AutoRouterImproved
4647
{
4748
return new AutoRouterImproved(
4849
[],
4950
$namespace,
5051
$this->collection->getDefaultController(),
5152
$this->collection->getDefaultMethod(),
52-
true,
53-
$httpVerb
53+
true
5454
);
5555
}
5656

@@ -61,7 +61,7 @@ public function testAutoRouteFindsDefaultControllerAndMethodGet(): void
6161
$router = $this->createNewAutoRouter();
6262

6363
[$directory, $controller, $method, $params]
64-
= $router->getRoute('/', 'get');
64+
= $router->getRoute('/', Method::GET);
6565

6666
$this->assertNull($directory);
6767
$this->assertSame('\\' . Index::class, $controller);
@@ -84,10 +84,10 @@ public function testAutoRouteFindsModuleDefaultControllerAndMethodGet()
8484

8585
$this->collection->setDefaultController('Index');
8686

87-
$router = $this->createNewAutoRouter('get', 'App/Controllers');
87+
$router = $this->createNewAutoRouter('App/Controllers');
8888

8989
[$directory, $controller, $method, $params]
90-
= $router->getRoute('test', 'get');
90+
= $router->getRoute('test', Method::GET);
9191

9292
$this->assertNull($directory);
9393
$this->assertSame('\\' . Index::class, $controller);
@@ -99,10 +99,10 @@ public function testAutoRouteFindsDefaultControllerAndMethodPost(): void
9999
{
100100
$this->collection->setDefaultController('Index');
101101

102-
$router = $this->createNewAutoRouter('post');
102+
$router = $this->createNewAutoRouter();
103103

104104
[$directory, $controller, $method, $params]
105-
= $router->getRoute('/', 'post');
105+
= $router->getRoute('/', Method::POST);
106106

107107
$this->assertNull($directory);
108108
$this->assertSame('\\' . Index::class, $controller);
@@ -115,7 +115,7 @@ public function testAutoRouteFindsControllerWithFileAndMethod(): void
115115
$router = $this->createNewAutoRouter();
116116

117117
[$directory, $controller, $method, $params]
118-
= $router->getRoute('mycontroller/somemethod', 'get');
118+
= $router->getRoute('mycontroller/somemethod', Method::GET);
119119

120120
$this->assertNull($directory);
121121
$this->assertSame('\\' . Mycontroller::class, $controller);
@@ -133,7 +133,7 @@ public function testFindsControllerAndMethodAndParam(): void
133133
$router = $this->createNewAutoRouter();
134134

135135
[$directory, $controller, $method, $params]
136-
= $router->getRoute('mycontroller/somemethod/a', 'get');
136+
= $router->getRoute('mycontroller/somemethod/a', Method::GET);
137137

138138
$this->assertNull($directory);
139139
$this->assertSame('\\' . Mycontroller::class, $controller);
@@ -155,15 +155,15 @@ public function testUriParamCountIsGreaterThanMethodParams(): void
155155

156156
$router = $this->createNewAutoRouter();
157157

158-
$router->getRoute('mycontroller/somemethod/a/b', 'get');
158+
$router->getRoute('mycontroller/somemethod/a/b', Method::GET);
159159
}
160160

161161
public function testAutoRouteFindsControllerWithFile(): void
162162
{
163163
$router = $this->createNewAutoRouter();
164164

165165
[$directory, $controller, $method, $params]
166-
= $router->getRoute('mycontroller', 'get');
166+
= $router->getRoute('mycontroller', Method::GET);
167167

168168
$this->assertNull($directory);
169169
$this->assertSame('\\' . Mycontroller::class, $controller);
@@ -176,7 +176,7 @@ public function testAutoRouteFindsControllerWithSubfolder(): void
176176
$router = $this->createNewAutoRouter();
177177

178178
[$directory, $controller, $method, $params]
179-
= $router->getRoute('subfolder/mycontroller/somemethod', 'get');
179+
= $router->getRoute('subfolder/mycontroller/somemethod', Method::GET);
180180

181181
$this->assertSame('Subfolder/', $directory);
182182
$this->assertSame('\\' . \CodeIgniter\Router\Controllers\Subfolder\Mycontroller::class, $controller);
@@ -194,7 +194,7 @@ public function testAutoRouteFindsControllerWithSubSubfolder()
194194
$router = $this->createNewAutoRouter();
195195

196196
[$directory, $controller, $method, $params]
197-
= $router->getRoute('subfolder/sub/mycontroller/somemethod', 'get');
197+
= $router->getRoute('subfolder/sub/mycontroller/somemethod', Method::GET);
198198

199199
$this->assertSame('Subfolder/Sub/', $directory);
200200
$this->assertSame('\\' . \CodeIgniter\Router\Controllers\Subfolder\Sub\Mycontroller::class, $controller);
@@ -207,7 +207,7 @@ public function testAutoRouteFindsDashedSubfolder(): void
207207
$router = $this->createNewAutoRouter();
208208

209209
[$directory, $controller, $method, $params]
210-
= $router->getRoute('dash-folder/mycontroller/somemethod', 'get');
210+
= $router->getRoute('dash-folder/mycontroller/somemethod', Method::GET);
211211

212212
$this->assertSame('Dash_folder/', $directory);
213213
$this->assertSame(
@@ -223,7 +223,7 @@ public function testAutoRouteFindsDashedController(): void
223223
$router = $this->createNewAutoRouter();
224224

225225
[$directory, $controller, $method, $params]
226-
= $router->getRoute('dash-folder/dash-controller/somemethod', 'get');
226+
= $router->getRoute('dash-folder/dash-controller/somemethod', Method::GET);
227227

228228
$this->assertSame('Dash_folder/', $directory);
229229
$this->assertSame('\\' . Dash_controller::class, $controller);
@@ -236,7 +236,7 @@ public function testAutoRouteFindsDashedMethod(): void
236236
$router = $this->createNewAutoRouter();
237237

238238
[$directory, $controller, $method, $params]
239-
= $router->getRoute('dash-folder/dash-controller/dash-method', 'get');
239+
= $router->getRoute('dash-folder/dash-controller/dash-method', Method::GET);
240240

241241
$this->assertSame('Dash_folder/', $directory);
242242
$this->assertSame('\\' . Dash_controller::class, $controller);
@@ -249,7 +249,7 @@ public function testAutoRouteFindsDefaultDashFolder(): void
249249
$router = $this->createNewAutoRouter();
250250

251251
[$directory, $controller, $method, $params]
252-
= $router->getRoute('dash-folder', 'get');
252+
= $router->getRoute('dash-folder', Method::GET);
253253

254254
$this->assertSame('Dash_folder/', $directory);
255255
$this->assertSame('\\' . Home::class, $controller);
@@ -262,7 +262,7 @@ public function testAutoRouteFallbackToDefaultMethod()
262262
$router = $this->createNewAutoRouter();
263263

264264
[$directory, $controller, $method, $params]
265-
= $router->getRoute('index/15', 'get');
265+
= $router->getRoute('index/15', Method::GET);
266266

267267
$this->assertNull($directory);
268268
$this->assertSame('\\' . Index::class, $controller);
@@ -280,7 +280,7 @@ public function testAutoRouteFallbackToDefaultControllerOneParam()
280280
$router = $this->createNewAutoRouter();
281281

282282
[$directory, $controller, $method, $params]
283-
= $router->getRoute('subfolder/15', 'get');
283+
= $router->getRoute('subfolder/15', Method::GET);
284284

285285
$this->assertSame('Subfolder/', $directory);
286286
$this->assertSame('\\' . \CodeIgniter\Router\Controllers\Subfolder\Home::class, $controller);
@@ -298,7 +298,7 @@ public function testAutoRouteFallbackToDefaultControllerTwoParams()
298298
$router = $this->createNewAutoRouter();
299299

300300
[$directory, $controller, $method, $params]
301-
= $router->getRoute('subfolder/15/20', 'get');
301+
= $router->getRoute('subfolder/15/20', Method::GET);
302302

303303
$this->assertSame('Subfolder/', $directory);
304304
$this->assertSame('\\' . \CodeIgniter\Router\Controllers\Subfolder\Home::class, $controller);
@@ -316,7 +316,7 @@ public function testAutoRouteFallbackToDefaultControllerNoParams()
316316
$router = $this->createNewAutoRouter();
317317

318318
[$directory, $controller, $method, $params]
319-
= $router->getRoute('subfolder', 'get');
319+
= $router->getRoute('subfolder', Method::GET);
320320

321321
$this->assertSame('Subfolder/', $directory);
322322
$this->assertSame('\\' . \CodeIgniter\Router\Controllers\Subfolder\Home::class, $controller);
@@ -335,7 +335,7 @@ public function testAutoRouteRejectsSingleDot(): void
335335

336336
$router = $this->createNewAutoRouter();
337337

338-
$router->getRoute('.', 'get');
338+
$router->getRoute('.', Method::GET);
339339
}
340340

341341
public function testAutoRouteRejectsDoubleDot(): void
@@ -344,7 +344,7 @@ public function testAutoRouteRejectsDoubleDot(): void
344344

345345
$router = $this->createNewAutoRouter();
346346

347-
$router->getRoute('..', 'get');
347+
$router->getRoute('..', Method::GET);
348348
}
349349

350350
public function testAutoRouteRejectsMidDot(): void
@@ -353,7 +353,7 @@ public function testAutoRouteRejectsMidDot(): void
353353

354354
$router = $this->createNewAutoRouter();
355355

356-
$router->getRoute('foo.bar', 'get');
356+
$router->getRoute('foo.bar', Method::GET);
357357
}
358358

359359
public function testRejectsDefaultControllerPath(): void
@@ -362,7 +362,7 @@ public function testRejectsDefaultControllerPath(): void
362362

363363
$router = $this->createNewAutoRouter();
364364

365-
$router->getRoute('home', 'get');
365+
$router->getRoute('home', Method::GET);
366366
}
367367

368368
public function testRejectsDefaultControllerAndDefaultMethodPath(): void
@@ -371,7 +371,7 @@ public function testRejectsDefaultControllerAndDefaultMethodPath(): void
371371

372372
$router = $this->createNewAutoRouter();
373373

374-
$router->getRoute('home/index', 'get');
374+
$router->getRoute('home/index', Method::GET);
375375
}
376376

377377
public function testRejectsDefaultMethodPath(): void
@@ -380,7 +380,7 @@ public function testRejectsDefaultMethodPath(): void
380380

381381
$router = $this->createNewAutoRouter();
382382

383-
$router->getRoute('mycontroller/index', 'get');
383+
$router->getRoute('mycontroller/index', Method::GET);
384384
}
385385

386386
public function testRejectsControllerWithRemapMethod(): void
@@ -392,7 +392,7 @@ public function testRejectsControllerWithRemapMethod(): void
392392

393393
$router = $this->createNewAutoRouter();
394394

395-
$router->getRoute('remap/test', 'get');
395+
$router->getRoute('remap/test', Method::GET);
396396
}
397397

398398
public function testRejectsURIWithUnderscoreFolder()
@@ -404,7 +404,7 @@ public function testRejectsURIWithUnderscoreFolder()
404404

405405
$router = $this->createNewAutoRouter();
406406

407-
$router->getRoute('dash_folder', 'get');
407+
$router->getRoute('dash_folder', Method::GET);
408408
}
409409

410410
public function testRejectsURIWithUnderscoreController()
@@ -416,7 +416,7 @@ public function testRejectsURIWithUnderscoreController()
416416

417417
$router = $this->createNewAutoRouter();
418418

419-
$router->getRoute('dash-folder/dash_controller/dash-method', 'get');
419+
$router->getRoute('dash-folder/dash_controller/dash-method', Method::GET);
420420
}
421421

422422
public function testRejectsURIWithUnderscoreMethod()
@@ -428,15 +428,15 @@ public function testRejectsURIWithUnderscoreMethod()
428428

429429
$router = $this->createNewAutoRouter();
430430

431-
$router->getRoute('dash-folder/dash-controller/dash_method', 'get');
431+
$router->getRoute('dash-folder/dash-controller/dash_method', Method::GET);
432432
}
433433

434434
public function testPermitsURIWithUnderscoreParam()
435435
{
436436
$router = $this->createNewAutoRouter();
437437

438438
[$directory, $controller, $method, $params]
439-
= $router->getRoute('mycontroller/somemethod/a_b', 'get');
439+
= $router->getRoute('mycontroller/somemethod/a_b', Method::GET);
440440

441441
$this->assertNull($directory);
442442
$this->assertSame('\\' . Mycontroller::class, $controller);
@@ -449,7 +449,7 @@ public function testDoesNotTranslateDashInParam()
449449
$router = $this->createNewAutoRouter();
450450

451451
[$directory, $controller, $method, $params]
452-
= $router->getRoute('mycontroller/somemethod/a-b', 'get');
452+
= $router->getRoute('mycontroller/somemethod/a-b', Method::GET);
453453

454454
$this->assertNull($directory);
455455
$this->assertSame('\\' . Mycontroller::class, $controller);

0 commit comments

Comments
 (0)