From 4c75c9469bbf9fc162030b6ed1412e9d80756db5 Mon Sep 17 00:00:00 2001 From: Kavalar Date: Mon, 7 Oct 2024 16:13:46 +0300 Subject: [PATCH] rest to menu --- kernel/CgRouteCollector.php | 9 +++++++++ kernel/RestController.php | 16 ++++++++-------- .../menu/controllers/MenuRestController.php | 16 ++++++++++++++++ kernel/modules/menu/routs/menu.php | 5 +++++ kernel/modules/post/routs/post.php | 15 +++------------ 5 files changed, 41 insertions(+), 20 deletions(-) create mode 100644 kernel/modules/menu/controllers/MenuRestController.php diff --git a/kernel/CgRouteCollector.php b/kernel/CgRouteCollector.php index 4cfa124..6c3b6d5 100755 --- a/kernel/CgRouteCollector.php +++ b/kernel/CgRouteCollector.php @@ -27,6 +27,15 @@ class CgRouteCollector extends RouteCollector return $this->addRoute(Route::ANY, $route . '/update/{id}', array_merge($handler, ['actionEdit']), $filters); } + public function rest($route, $handler, array $filters = []): CgRouteCollector + { + $this->addRoute(Route::GET, $route, array_merge($handler, ['actionIndex']), $filters); + $this->addRoute(Route::GET, $route . '/{id}', array_merge($handler, ['actionView']), $filters); + $this->addRoute(Route::POST, $route . '/', array_merge($handler, ['actionStore']), $filters); + $this->addRoute(Route::DELETE, $route . '/delete', array_merge($handler, ['actionDelete']), $filters); + return $this->addRoute(Route::ANY, $route . '/update/{id}', array_merge($handler, ['actionEdit']), $filters); + } + public function authorization($route, $handler, array $filters = []) { //TODO diff --git a/kernel/RestController.php b/kernel/RestController.php index fad2037..7f5a355 100644 --- a/kernel/RestController.php +++ b/kernel/RestController.php @@ -74,17 +74,17 @@ class RestController } - #[NoReturn] public function actionCreate(): void + #[NoReturn] public function actionStore(): void { + $request = new Request(); $data = $request->post(); - $data = [ - 'title' => 'title' - ]; - Debug::prn($data); - $model = $this->model; - Debug::prn($model->toArray()); - $model->save($data); + foreach ($this->model->getFillable() as $item){ + $this->model->{$item} = $data[$item] ?? null; + } + $this->model->save(); + + $this->renderApi($this->model->toArray()); } #[NoReturn] protected function renderApi(array $data): void diff --git a/kernel/modules/menu/controllers/MenuRestController.php b/kernel/modules/menu/controllers/MenuRestController.php new file mode 100644 index 0000000..d2cc2a1 --- /dev/null +++ b/kernel/modules/menu/controllers/MenuRestController.php @@ -0,0 +1,16 @@ +model = new Menu(); + } + +} \ No newline at end of file diff --git a/kernel/modules/menu/routs/menu.php b/kernel/modules/menu/routs/menu.php index 14b907b..753f239 100644 --- a/kernel/modules/menu/routs/menu.php +++ b/kernel/modules/menu/routs/menu.php @@ -1,6 +1,7 @@ group(["prefix" => "admin"], function (RouteCollector $router) { @@ -16,4 +17,8 @@ App::$collector->group(["prefix" => "admin"], function (RouteCollector $router) App::$collector->get('/delete/{id}', [\kernel\modules\menu\controllers\MenuController::class, 'actionDelete']); }); }); +}); + +App::$collector->group(["prefix" => "api"], function (CgRouteCollector $router){ + $router->rest("menu", [\kernel\modules\menu\controllers\MenuRestController::class]); }); \ No newline at end of file diff --git a/kernel/modules/post/routs/post.php b/kernel/modules/post/routs/post.php index f98082e..8d4755a 100644 --- a/kernel/modules/post/routs/post.php +++ b/kernel/modules/post/routs/post.php @@ -1,6 +1,7 @@ group(["prefix" => "admin"], function (RouteCollector $router){ }); }); -App::$collector->group(["prefix" => "api"], function (RouteCollector $router){ - App::$collector->group(["prefix" => "post"], function (RouteCollector $router){ - App::$collector->get('/', [\kernel\modules\post\controllers\PostRestController::class, 'actionIndex']); - App::$collector->get('/{id}', [\kernel\modules\post\controllers\PostRestController::class, 'actionView']); -// App::$collector->get('/page/{page_number}', [\kernel\modules\post\controllers\PostController::class, 'actionIndex']); - App::$collector->post('/create', [\kernel\modules\post\controllers\PostRestController::class, 'actionCreate']); -// App::$collector->post("/", [\kernel\modules\post\controllers\PostController::class, 'actionAdd']); -// App::$collector->get('/{id}', [\kernel\modules\post\controllers\PostController::class, 'actionView']); -// App::$collector->any('/update/{id}', [\kernel\modules\post\controllers\PostController::class, 'actionUpdate']); -// App::$collector->any("/edit/{id}", [\kernel\modules\post\controllers\PostController::class, 'actionEdit']); - App::$collector->get('/delete/{id}', [\kernel\modules\post\controllers\PostRestController::class, 'actionDelete']); - }); +App::$collector->group(["prefix" => "api"], function (CgRouteCollector $router){ + $router->rest("post", [\kernel\modules\post\controllers\PostRestController::class]); }); \ No newline at end of file