From e1a163ef8c5329c7fd45546b8869130e7b85d7bd Mon Sep 17 00:00:00 2001 From: imnavajas Date: Wed, 15 Jan 2025 21:05:12 +0100 Subject: [PATCH] Implementado filtro CORS y ejemplo de resources para la API --- ci4/app/Config/Filters.php | 1 + ci4/app/Config/Routes.php | 27 ++++- ci4/app/Controllers/API/ItemsController.php | 103 ++++++++++++++++++++ ci4/app/Filters/Cors.php | 56 +++++++++++ ci4/app/Models/API/ItemModel.php | 46 +++++++++ 5 files changed, 228 insertions(+), 5 deletions(-) create mode 100644 ci4/app/Controllers/API/ItemsController.php create mode 100644 ci4/app/Filters/Cors.php create mode 100644 ci4/app/Models/API/ItemModel.php diff --git a/ci4/app/Config/Filters.php b/ci4/app/Config/Filters.php index f17d1127..178fffba 100755 --- a/ci4/app/Config/Filters.php +++ b/ci4/app/Config/Filters.php @@ -26,6 +26,7 @@ class Filters extends BaseConfig 'honeypot' => Honeypot::class, 'invalidchars' => InvalidChars::class, 'secureheaders' => SecureHeaders::class, + 'cors' => \App\Filters\Cors::class ]; /** diff --git a/ci4/app/Config/Routes.php b/ci4/app/Config/Routes.php index 908e1388..25fd341d 100644 --- a/ci4/app/Config/Routes.php +++ b/ci4/app/Config/Routes.php @@ -918,14 +918,31 @@ $routes->group('produccion', ['namespace' => 'App\Controllers\Produccion'], func */ $routes->post('auth/jwt', '\App\Controllers\Sistema\AuthAPIController::jwtLogin'); -$routes->group('api', ['filter' => 'jwt'], static function ($routes) { - $routes->get('test', 'Test::echo'); - // ... -}); +$routes->group( + 'api', + [ + 'namespace' => 'App\Controllers\API', + 'filter' => 'jwt' + ], + static function ($routes) { + + $routes->resource('items', [ + 'controller' => 'ItemsController', + 'filter' => 'cors' + ]); + + $routes->options('items', static function () { }); + $routes->options('items/(:any)', static function () { }); + + + + // ... + } +); /* * -------------------------------------------------------------------- - * Translation + * Translation * -------------------------------------------------------------------- */ $routes->group('translate', ['namespace' => 'App\Controllers'], function ($routes) { diff --git a/ci4/app/Controllers/API/ItemsController.php b/ci4/app/Controllers/API/ItemsController.php new file mode 100644 index 00000000..b4ccbfe8 --- /dev/null +++ b/ci4/app/Controllers/API/ItemsController.php @@ -0,0 +1,103 @@ +findAll(); + return $this->respond($data); + } + + public function show($id = null) + { + $model = new ItemModel(); + $data = $model->find(['id' => $id]); + if (!$data) + return $this->failNotFound('No Data Found'); + return $this->respond($data[0]); + } + + public function create() + { + helper(['form']); + $rules = [ + 'title' => 'required', + 'price' => 'required' + ]; + $data = [ + 'title' => $this->request->getVar('title'), + 'price' => $this->request->getVar('price') + ]; + + if (!$this->validate($rules)) + return $this->fail($this->validator->getErrors()); + $model = new ItemModel(); + $model->save($data); + $response = [ + 'status' => 201, + 'error' => null, + 'messages' => [ + 'success' => 'Data Inserted' + ] + ]; + return $this->respondCreated($response); + } + + public function update($id = null) + { + helper(['form']); + $rules = [ + 'title' => 'required', + 'price' => 'required' + ]; + $data = [ + 'title' => $this->request->getVar('title'), + 'price' => $this->request->getVar('price') + ]; + + if (!$this->validate($rules)) + return $this->fail($this->validator->getErrors()); + $model = new ItemModel(); + $find = $model->find(['id' => $id]); + if (!$find) + return $this->failNotFound('No Data Found'); + $model->update($id, $data); + + $response = [ + 'status' => 200, + 'error' => null, + 'messages' => [ + 'success' => 'Data updated' + ] + ]; + return $this->respond($response); + } + + public function delete($id = null) + { + $model = new ItemModel(); + $find = $model->find(['id' => $id]); + if (!$find) + return $this->failNotFound('No Data Found'); + $model->delete($id); + + $response = [ + 'status' => 200, + 'error' => null, + 'messages' => [ + 'success' => 'Data deleted' + ] + ]; + return $this->respond($response); + } +} diff --git a/ci4/app/Filters/Cors.php b/ci4/app/Filters/Cors.php new file mode 100644 index 00000000..19c78a38 --- /dev/null +++ b/ci4/app/Filters/Cors.php @@ -0,0 +1,56 @@ +