Skip to content

Commit 79dc792

Browse files
committed
fix: add middleware support to apiResource
1 parent 5187459 commit 79dc792

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

src/Router.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -378,15 +378,29 @@ public static function resource(string $pattern, $controller)
378378
* - `/posts/{id}/delete` - POST | DELETE - Controller@destroy
379379
*
380380
* @param string $pattern The base route to use eg: /post
381-
* @param string $controller to handle route eg: PostController
381+
* @param array|string $controller to handle route eg: PostController
382382
*/
383-
public static function apiResource(string $pattern, string $controller)
383+
public static function apiResource(string $pattern, $controller)
384384
{
385+
if (is_array($controller)) {
386+
$controllerToCall = $controller[0];
387+
388+
$controller[0] = function () use ($pattern, $controllerToCall) {
389+
static::apiResource('/', $controllerToCall);
390+
};
391+
392+
return static::group($pattern, $controller);
393+
}
394+
385395
static::match('GET|HEAD', $pattern, "$controller@index");
386396
static::post($pattern, "$controller@store");
397+
static::match('GET|HEAD', "$pattern/{id}", "$controller@show");
398+
static::match('DELETE', "$pattern/{id}", "$controller@destroy");
399+
static::match('PUT|PATCH', "$pattern/{id}", "$controller@update");
400+
401+
// still keeping DELETE and PUT|PATCH so earlier versions of leaf apps don't break
387402
static::match('POST|DELETE', "$pattern/{id}/delete", "$controller@destroy");
388403
static::match('POST|PUT|PATCH', "$pattern/{id}/edit", "$controller@update");
389-
static::match('GET|HEAD', "$pattern/{id}", "$controller@show");
390404
}
391405

392406
/**

0 commit comments

Comments
 (0)