From 1206a963681a0cce6db3eb6848bcf293c7667bec Mon Sep 17 00:00:00 2001 From: mychidarko Date: Sat, 17 Sep 2022 21:36:07 +0000 Subject: [PATCH] :bug: add apiResource method --- src/Router.php | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/Router.php b/src/Router.php index b994b56..6dcf195 100644 --- a/src/Router.php +++ b/src/Router.php @@ -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 *