Update Cors Kembali ke Awal & API get all Zones

This commit is contained in:
mikael-zakaria 2025-10-23 09:02:24 +07:00
parent 34449ec836
commit 6a77f66e87
5 changed files with 73 additions and 61 deletions

View File

@ -74,7 +74,7 @@ class Filters extends BaseFilters
*/ */
public array $globals = [ public array $globals = [
'before' => [ 'before' => [
'cors', // 'cors',
'auth' => [ 'except' => [ 'auth' => [ 'except' => [
'auth/*', 'lqms/*', 'key/*', 'api/*' 'auth/*', 'lqms/*', 'key/*', 'api/*'
]] ]]

View File

@ -256,10 +256,9 @@ $routes->group('', ['filter' => 'cors'], function($routes) {
}); });
// REST API // REST API
$routes->get('/api/provinces', 'Api\ZonesApi::getProvinces'); $routes->get('/api/zones', 'Api\ZonesApi::getAll');
$routes->get('/api/cities', 'Api\ZonesApi::getCities'); // $routes->get('/api/provinces', 'Api\ZonesApi::getProvinces');
// $routes->get('/api/zones', 'Api::ZonesApi/'); // $routes->get('/api/cities', 'Api\ZonesApi::getCities');
// $routes->get('/api/getProductList', 'Api::getProductList');
$routes->get('/api/getProductAlias', 'Api::getProductAlias'); $routes->get('/api/getProductAlias', 'Api::getProductAlias');
$routes->get('/api/getProductSites', 'Api::getProductSites'); $routes->get('/api/getProductSites', 'Api::getProductSites');

View File

@ -14,27 +14,34 @@ class ZonesApi extends BaseController {
$this->model = new ZonesModel(); $this->model = new ZonesModel();
} }
public function getProvinces() { public function getAll() {
// $filters = [ $rows = $this->model->getAll();
// 'zoneid' => $this->request->getVar('zoneid') ?? null,
// 'zonename' => $this->request->getVar('zonename') ?? null
// ];
$rows = $this->model->getAllProvinces();
if (empty($rows)) { return $this->respond([ 'status' => 'success', 'message' => "data not found", 'data' => [] ], 200); } if (empty($rows)) { return $this->respond([ 'status' => 'success', 'message' => "data not found", 'data' => [] ], 200); }
return $this->respond([ 'status' => 'success', 'message'=> "Data fetched successfully", 'data' => $rows ], 200); return $this->respond([ 'status' => 'success', 'message'=> "Data fetched successfully", 'data' => $rows ], 200);
} }
public function getCities() { // public function getProvinces() {
// // $filters = [
// // 'zoneid' => $this->request->getVar('zoneid') ?? null,
// // 'zonename' => $this->request->getVar('zonename') ?? null
// // ];
$filter = [ // $rows = $this->model->getAllProvinces();
'zoneid' => $this->request->getVar('zoneid') ?? null
];
$rows = $this->model->getAllCities($filter);
if (empty($rows)) { return $this->respond([ 'status' => 'success', 'message' => "data not found", 'data' => [] ], 200); } // if (empty($rows)) { return $this->respond([ 'status' => 'success', 'message' => "data not found", 'data' => [] ], 200); }
return $this->respond([ 'status' => 'success', 'message'=> "Data fetched successfully", 'data' => $rows ], 200); // return $this->respond([ 'status' => 'success', 'message'=> "Data fetched successfully", 'data' => $rows ], 200);
} // }
// public function getCities() {
// $filter = [
// 'zoneid' => $this->request->getVar('zoneid') ?? null
// ];
// $rows = $this->model->getAllCities($filter);
// if (empty($rows)) { return $this->respond([ 'status' => 'success', 'message' => "data not found", 'data' => [] ], 200); }
// return $this->respond([ 'status' => 'success', 'message'=> "Data fetched successfully", 'data' => $rows ], 200);
// }
} }

View File

@ -6,46 +6,48 @@ use CodeIgniter\Filters\FilterInterface;
class Cors implements FilterInterface class Cors implements FilterInterface
{ {
protected $allowedOrigins = [ // CORS BARU
'http://localhost:5173', // protected $allowedOrigins = [
'https://clqms01.services-summit.my.id', // 'http://localhost:5173',
]; // 'https://clqms01.services-summit.my.id',
// ];
public function before(RequestInterface $request, $arguments = null) public function before(RequestInterface $request, $arguments = null)
{ {
// header('Access-Control-Allow-Origin: *'); // CORS LAMA
// header('Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE'); header('Access-Control-Allow-Origin: *');
// header('Access-Control-Allow-Headers: Content-Type, Authorization, X-Requested-With, X-CSRF-TOKEN'); header('Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE');
header('Access-Control-Allow-Headers: Content-Type, Authorization, X-Requested-With, X-CSRF-TOKEN');
// Handle preflight requests // Handle preflight requests
// if ($request->getMethod() === 'options') { // if ($request->getMethod() === 'options') {
// header('HTTP/1.1 200 OK'); // header('HTTP/1.1 200 OK');
// exit(); // exit();
// } // }
// CORS BARU
// log_message('debug', 'Cors Filter Triggered First'); // log_message('debug', 'Cors Filter Triggered First');
$origin = $_SERVER['HTTP_ORIGIN'] ?? ''; // $origin = $_SERVER['HTTP_ORIGIN'] ?? '';
$response = service('response'); // $response = service('response');
if (in_array($origin, $this->allowedOrigins)) { // if (in_array($origin, $this->allowedOrigins)) {
$response->setHeader('Access-Control-Allow-Origin', $origin); // $response->setHeader('Access-Control-Allow-Origin', $origin);
$response->setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, PATCH, DELETE, OPTIONS'); // $response->setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, PATCH, DELETE, OPTIONS');
$response->setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization, X-Requested-With, Accept, Origin, Cache-Control, Pragma, X-CSRF-TOKEN'); // $response->setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization, X-Requested-With, Accept, Origin, Cache-Control, Pragma, X-CSRF-TOKEN');
// $response->setHeader('Access-Control-Allow-Headers', '*'); // // $response->setHeader('Access-Control-Allow-Headers', '*');
$response->setHeader('Access-Control-Allow-Credentials', 'true'); // $response->setHeader('Access-Control-Allow-Credentials', 'true');
} // }
// Tangani preflight OPTIONS dengan return response // Tangani preflight OPTIONS dengan return response
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') { // if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
// log_message('debug', 'Cors Filter Triggered OK'); // // log_message('debug', 'Cors Filter Triggered OK');
return $response->setStatusCode(200)->setBody('OK'); // return $response->setStatusCode(200)->setBody('OK');
} // }
// log_message('debug', 'Cors Filter Triggered Second'); // log_message('debug', 'Cors Filter Triggered Second');
} }
public function after(RequestInterface $request, ResponseInterface $response, $arguments = null) public function after(RequestInterface $request, ResponseInterface $response, $arguments = null)
{ {
// No actions required after the request // No actions required after the request
return $response; // return $response;
} }
} }

View File

@ -8,27 +8,31 @@ class ZonesModel extends Model {
protected $primaryKey = 'zoneid'; protected $primaryKey = 'zoneid';
protected $allowedFields = [ 'zonecode', 'zoneclass', 'parentzoneid', 'zonename' ]; protected $allowedFields = [ 'zonecode', 'zoneclass', 'parentzoneid', 'zonename' ];
public function getAllProvinces() { public function getAll() {
$this->select('zoneid, zonename')->where('parentzoneid IS NULL', null, false);
// if (!empty($filters['zoneid'])) {
// $this->where('zoneid', $filters['zoneid']);
// }
// if (!empty($filters['zonename'])) {
// $this->like('zonename', $filters['zonename'], 'both');
// }
return $this->findAll(); return $this->findAll();
} }
public function getAllCities($filter = []) { // public function getAllProvinces() {
$rows = $this->select('zoneid, zonename')->where('parentzoneid IS NOT NULL', null, false); // $this->select('zoneid, zonename')->where('parentzoneid IS NULL', null, false);
if (!empty($filter['zoneid'])) { // // if (!empty($filters['zoneid'])) {
$this->where('parentzoneid', $filter['zoneid']); // // $this->where('zoneid', $filters['zoneid']);
} // // }
// // if (!empty($filters['zonename'])) {
// // $this->like('zonename', $filters['zonename'], 'both');
// // }
return $this->findAll(); // return $this->findAll();
} // }
// public function getAllCities($filter = []) {
// $rows = $this->select('zoneid, zonename')->where('parentzoneid IS NOT NULL', null, false);
// if (!empty($filter['zoneid'])) {
// $this->where('parentzoneid', $filter['zoneid']);
// }
// return $this->findAll();
// }
} }