Custom Methods
Pengenalan Custom Methods
Custom Methods memungkinkan Anda untuk mendefinisikan endpoint API khusus di luar operasi CRUD standar. Fitur ini sangat berguna untuk operasi kompleks atau bisnis logic yang spesifik.
Method yang Tersedia
GET|POST /api/Info/cek
Memeriksa status informasi.
Response:
{
"status": "success",
"message": "info berhasil",
"data": {
// Data request
}
}
GET /api/Info/status
Mendapatkan status sistem secara detail.
Response:
{
"server": {
"php_version": "8.x.x",
"server_software": "Apache/2.4.x",
"server_protocol": "HTTP/1.1",
"request_time": "2024-03-21 10:00:00",
"timezone": "UTC"
},
"application": {
"environment": "production",
"debug_mode": false,
"version": "1.0.0"
}
}
GET /api/Info/version
Mendapatkan informasi versi API.
Response:
{
"api_version": "1.0.0",
"framework_version": "1.0.0",
"release_date": "2024-03-20",
"supported_versions": ["1.0.0"],
"deprecated_versions": []
}
Implementasi Custom Method
Contoh Controller:
class InfoController extends ApiController {
// Method untuk endpoint /api/Info/status
public function status() {
return $this->response([
'server' => $this->getServerInfo(),
'application' => $this->getAppInfo()
]);
}
// Method untuk endpoint /api/Info/version
public function version() {
return $this->response([
'api_version' => '1.0.0',
'framework_version' => '1.0.0'
]);
}
// Method untuk endpoint /api/Info/cek
public function cek($params = null) {
// Logic pemeriksaan
return $this->response([
'status' => 'success',
'message' => 'Pemeriksaan berhasil'
]);
}
}
Best Practices
- Gunakan nama method yang deskriptif dan mengikuti konvensi
- Dokumentasikan parameter yang diperlukan
- Tetap konsisten dengan format response standar
- Implementasikan validasi input yang tepat
- Gunakan HTTP method yang sesuai (GET untuk read, POST untuk write)
Contoh Penggunaan
JavaScript:
// Mengecek status
fetch('/api/Info/status')
.then(response => response.json())
.then(data => console.log(data));
// Mendapatkan versi
fetch('/api/Info/version')
.then(response => response.json())
.then(data => console.log(data));
// Melakukan pengecekan dengan parameter
fetch('/api/Info/cek/123', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
type: 'full'
})
})
.then(response => response.json())
.then(data => console.log(data));