Update File Custom Cors All Method
This commit is contained in:
parent
aedbba4357
commit
27856b7d54
@ -3,7 +3,7 @@
|
|||||||
namespace Config;
|
namespace Config;
|
||||||
|
|
||||||
use CodeIgniter\Config\Filters as BaseFilters;
|
use CodeIgniter\Config\Filters as BaseFilters;
|
||||||
use CodeIgniter\Filters\Cors;
|
// use CodeIgniter\Filters\Cors;
|
||||||
use CodeIgniter\Filters\CSRF;
|
use CodeIgniter\Filters\CSRF;
|
||||||
use CodeIgniter\Filters\DebugToolbar;
|
use CodeIgniter\Filters\DebugToolbar;
|
||||||
use CodeIgniter\Filters\ForceHTTPS;
|
use CodeIgniter\Filters\ForceHTTPS;
|
||||||
@ -30,7 +30,8 @@ class Filters extends BaseFilters
|
|||||||
'honeypot' => Honeypot::class,
|
'honeypot' => Honeypot::class,
|
||||||
'invalidchars' => InvalidChars::class,
|
'invalidchars' => InvalidChars::class,
|
||||||
'secureheaders' => SecureHeaders::class,
|
'secureheaders' => SecureHeaders::class,
|
||||||
'cors' => Cors::class,
|
// 'cors' => Cors::class,
|
||||||
|
'cors' => \App\Filters\Cors::class,
|
||||||
'forcehttps' => ForceHTTPS::class,
|
'forcehttps' => ForceHTTPS::class,
|
||||||
'pagecache' => PageCache::class,
|
'pagecache' => PageCache::class,
|
||||||
'performance' => PerformanceMetrics::class,
|
'performance' => PerformanceMetrics::class,
|
||||||
@ -69,7 +70,7 @@ class Filters extends BaseFilters
|
|||||||
*/
|
*/
|
||||||
public array $globals = [
|
public array $globals = [
|
||||||
'before' => [
|
'before' => [
|
||||||
'cors',
|
'cors'
|
||||||
// 'honeypot',
|
// 'honeypot',
|
||||||
// 'csrf',
|
// 'csrf',
|
||||||
// 'invalidchars',
|
// 'invalidchars',
|
||||||
|
|||||||
@ -39,10 +39,16 @@ class Patient extends Controller {
|
|||||||
$filteredPatients = $builder->get()->getResultArray();
|
$filteredPatients = $builder->get()->getResultArray();
|
||||||
|
|
||||||
if (empty($filteredPatients)) {
|
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) {
|
public function show($id = null) {
|
||||||
@ -50,10 +56,17 @@ class Patient extends Controller {
|
|||||||
$patient = $builder->where('pat_num', $id)->get()->getRowArray();
|
$patient = $builder->where('pat_num', $id)->get()->getRowArray();
|
||||||
|
|
||||||
if (empty($patient)) {
|
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() {
|
public function create() {
|
||||||
@ -115,7 +128,7 @@ class Patient extends Controller {
|
|||||||
|
|
||||||
return $this->respondCreated([
|
return $this->respondCreated([
|
||||||
'message' => 'Patient created successfully',
|
'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