Update File Custom Cors All Method
This commit is contained in:
parent
aedbba4357
commit
27856b7d54
@ -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',
|
||||
|
||||
@ -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
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
42
app/Filters/Cors.php
Normal file
42
app/Filters/Cors.php
Normal file
@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filters;
|
||||
|
||||
use CodeIgniter\HTTP\RequestInterface;
|
||||
use CodeIgniter\HTTP\ResponseInterface;
|
||||
use CodeIgniter\Filters\FilterInterface;
|
||||
|
||||
class Cors implements FilterInterface
|
||||
{
|
||||
protected $allowedOrigins = [
|
||||
'http://localhost:5173',
|
||||
'https://clqms01.services-summit.my.id',
|
||||
];
|
||||
|
||||
public function before(RequestInterface $request, $arguments = null)
|
||||
{
|
||||
log_message('debug', 'Cors Filter Triggered First');
|
||||
$origin = $_SERVER['HTTP_ORIGIN'] ?? '';
|
||||
$response = service('response');
|
||||
|
||||
if (in_array($origin, $this->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;
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user