Skip to content

Commit

Permalink
fix: add middleware support to apiResource
Browse files Browse the repository at this point in the history
  • Loading branch information
mychidarko committed Dec 6, 2024
1 parent 5187459 commit 79dc792
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions src/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -378,15 +378,29 @@ public static function resource(string $pattern, $controller)
* - `/posts/{id}/delete` - POST | DELETE - Controller@destroy
*
* @param string $pattern The base route to use eg: /post
* @param string $controller to handle route eg: PostController
* @param array|string $controller to handle route eg: PostController
*/
public static function apiResource(string $pattern, string $controller)
public static function apiResource(string $pattern, $controller)
{
if (is_array($controller)) {
$controllerToCall = $controller[0];

$controller[0] = function () use ($pattern, $controllerToCall) {
static::apiResource('/', $controllerToCall);
};

return static::group($pattern, $controller);
}

static::match('GET|HEAD', $pattern, "$controller@index");
static::post($pattern, "$controller@store");
static::match('GET|HEAD', "$pattern/{id}", "$controller@show");
static::match('DELETE', "$pattern/{id}", "$controller@destroy");
static::match('PUT|PATCH', "$pattern/{id}", "$controller@update");

// still keeping DELETE and PUT|PATCH so earlier versions of leaf apps don't break
static::match('POST|DELETE', "$pattern/{id}/delete", "$controller@destroy");
static::match('POST|PUT|PATCH', "$pattern/{id}/edit", "$controller@update");
static::match('GET|HEAD', "$pattern/{id}", "$controller@show");
}

/**
Expand Down

0 comments on commit 79dc792

Please sign in to comment.