From 27856b7d5409d84ef37fcb8b8d260c4b3b0b7394 Mon Sep 17 00:00:00 2001 From: mikael-zakaria Date: Wed, 16 Jul 2025 09:19:47 +0700 Subject: [PATCH] Update File Custom Cors All Method --- app/Config/Filters.php | 7 ++++--- app/Controllers/Patient.php | 23 +++++++++++++++----- app/Filters/Cors.php | 42 +++++++++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 8 deletions(-) create mode 100644 app/Filters/Cors.php diff --git a/app/Config/Filters.php b/app/Config/Filters.php index 859c192..9f335d2 100644 --- a/app/Config/Filters.php +++ b/app/Config/Filters.php @@ -3,7 +3,7 @@ namespace Config; use CodeIgniter\Config\Filters as BaseFilters; -use CodeIgniter\Filters\Cors; +// use CodeIgniter\Filters\Cors; use CodeIgniter\Filters\CSRF; use CodeIgniter\Filters\DebugToolbar; use CodeIgniter\Filters\ForceHTTPS; @@ -30,7 +30,8 @@ class Filters extends BaseFilters 'honeypot' => Honeypot::class, 'invalidchars' => InvalidChars::class, 'secureheaders' => SecureHeaders::class, - 'cors' => Cors::class, + // 'cors' => Cors::class, + 'cors' => \App\Filters\Cors::class, 'forcehttps' => ForceHTTPS::class, 'pagecache' => PageCache::class, 'performance' => PerformanceMetrics::class, @@ -69,7 +70,7 @@ class Filters extends BaseFilters */ public array $globals = [ 'before' => [ - 'cors', + 'cors' // 'honeypot', // 'csrf', // 'invalidchars', diff --git a/app/Controllers/Patient.php b/app/Controllers/Patient.php index 52431c7..00c0466 100644 --- a/app/Controllers/Patient.php +++ b/app/Controllers/Patient.php @@ -39,10 +39,16 @@ class Patient extends Controller { $filteredPatients = $builder->get()->getResultArray(); if (empty($filteredPatients)) { - return $this->failNotFound('No patient records found matching the criteria.'); + return $this->failNotFound([ + 'status' => 'error', + 'message' => 'No patient records found matching the criteria.' + ]); } - return $this->respond($filteredPatients); + return $this->respond([ + 'status' => 'success', + 'data' => $filteredPatients, + ]); } public function show($id = null) { @@ -50,10 +56,17 @@ class Patient extends Controller { $patient = $builder->where('pat_num', $id)->get()->getRowArray(); if (empty($patient)) { - return $this->failNotFound('Patient with ID ' . $id . ' not found.'); + return $this->failNotFound([ + 'status' => 'error', + 'message' => 'Patient with ID ' . $id . ' not found.' + ]); } - return $this->respond($patient); + return $this->respond([ + 'status' => 'success', + 'data' => $patient, + ]); + } public function create() { @@ -115,7 +128,7 @@ class Patient extends Controller { return $this->respondCreated([ 'message' => 'Patient created successfully', - 'pat_id' => $newPatientId + 'pat_id' => $newPatientId ]); } diff --git a/app/Filters/Cors.php b/app/Filters/Cors.php new file mode 100644 index 0000000..f0377ab --- /dev/null +++ b/app/Filters/Cors.php @@ -0,0 +1,42 @@ +allowedOrigins)) { + $response->setHeader('Access-Control-Allow-Origin', $origin); + $response->setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS'); + $response->setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization, X-Requested-With'); + $response->setHeader('Access-Control-Allow-Credentials', 'true'); + } + + // Tangani preflight OPTIONS dengan return response + if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') { + log_message('debug', 'Cors Filter Triggered OK'); + return $response->setStatusCode(200)->setBody('OK'); + } + log_message('debug', 'Cors Filter Triggered Second'); + } + + public function after(RequestInterface $request, ResponseInterface $response, $arguments = null) + { + log_message('debug', 'Cors Filter Triggered Thrid'); + return $response; + } +}