Skip to content

Commit

Permalink
Allow the index_ prefix as an alternative to get_ prefixes
Browse files Browse the repository at this point in the history
Consider the following endpoints we’d want to create:

```http
GET /conversations/123/participants
GET /conversations/123/participants/1
```

Previously we would be unable to create these routes because they would
both have to be called `get_participants`.

This change allows you to also prefix GET methods with `index_`. The
prefix works the same way as `get_` but is a lower priority. They are
differentiated by different parameter counts.

The above example can be done with the following methods:

```php
public function index_participants($id) {
}

public function get_participants($id, $userID) {
}
```
  • Loading branch information
tburry committed Mar 6, 2018
1 parent 5852e5d commit 879a9da
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
10 changes: 9 additions & 1 deletion library/Garden/Web/ResourceRoute.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ private function checkMethodCase($method, $compare, $obj, $notice = false) {
* @return callable|null Returns the method callback or null if it doesn't.
*/
private function findMethod($controller, $methodName) {
$regex = '`^(get|post|patch|put|options|delete)(_|$)`i';
$regex = '`^(get|index|post|patch|put|options|delete)(_|$)`i';

// Getters and setters aren't found.
if (!(preg_match($regex, $methodName) || strcasecmp($methodName, 'index') === 0)) {
Expand Down Expand Up @@ -381,10 +381,18 @@ private function getControllerMethodNames($method, $pathArgs) {
if (isset($pathArgs[0])) {
$name = lcfirst($this->filterName($pathArgs[0]));
$result[] = ["{$method}_{$name}", 0];

if ($method === 'get') {
$result[] = ["index_{$name}", 0];
}
}
if (isset($pathArgs[1])) {
$name = lcfirst($this->filterName($pathArgs[1]));
$result[] = ["{$method}_{$name}", 1];

if ($method === 'get') {
$result[] = ["index_{$name}", 1];
}
}

$result[] = [$method, null];
Expand Down
6 changes: 6 additions & 0 deletions tests/Library/Garden/Web/ResourceRouteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ public function provideKnownRoutes() {

'no mapping' => ['POST', '/discussions/no-map/a/b/c?f=b', [$dc, 'post_noMap'], ['query' => 'a', 'body' => 'b', 'data' => 'c']],

// Nested get and index
'index /sub' => ['GET', '/discussions/sub', [$dc, 'index_sub'], []],
'get /sub/:arg' => ['GET', '/discussions/sub/abc', [$dc, 'get_sub'], ['arg' => 'abc']],
'index /:id/idsub' => ['GET', '/discussions/123/idsub', [$dc, 'index_idsub'], ['id' => '123']],
'get /:id/idsub/:id2' => ['GET', '/discussions/123/idsub/abc', [$dc, 'get_idsub'], ['id' => '123', 'id2' => 'abc']],

// Special routes are special.
'bad index' => ['GET', '/discussions/index', null],
'bad get' => ['GET', '/discussions/get/123', null],
Expand Down
16 changes: 16 additions & 0 deletions tests/fixtures/src/DiscussionsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,20 @@ public function get_path4($a, $path, $b) {

public function get_article($path, $page = '') {
}

public function index_sub() {

}

public function get_sub($arg) {

}

public function get_idsub($id, $id2) {

}

public function index_idsub($id) {

}
}

0 comments on commit 879a9da

Please sign in to comment.