Skip to content

Commit

Permalink
🐛 add apiResource method
Browse files Browse the repository at this point in the history
  • Loading branch information
mychidarko committed Sep 17, 2022
1 parent 7847e5a commit 1206a96
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,29 @@ public static function resource(string $pattern, string $controller)
static::match('GET|HEAD', "$pattern/{id}", "$controller@show");
}

/**
* Create a resource route for using controllers without the create and edit actions.
*
* This creates a routes that implement CRUD functionality in a controller
* `/posts` creates:
* - `/posts` - GET | HEAD - Controller@index
* - `/posts` - POST - Controller@store
* - `/posts/{id}` - GET | HEAD - Controller@show
* - `/posts/{id}/edit` - POST | PUT | PATCH - Controller@update
* - `/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
*/
public static function apiResource(string $pattern, string $controller)
{
static::match('GET|HEAD', $pattern, "$controller@index");
static::post($pattern, "$controller@store");
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");
}

/**
* Redirect to another route
*
Expand Down

0 comments on commit 1206a96

Please sign in to comment.