artiframe make:api
The artiframe make:api command adds a new JSON API endpoint to your project.
It offers two different API architectures: standart (single-purpose) and switch-case (multi-purpose CRUD).
The generated files come pre-equipped with ArtiFrame's security layer (ApiControl.php) and CSRF protection right out of the box.
Usage
$ artiframe make:api [type] [path/filename]
| Parameter | Required | Description |
|---|---|---|
type | Yes | Must be either standart or switch-case |
path/filename | Yes | The target path for the API file (can be written without an extension) |
API Types
1. standart — Single-Purpose Endpoint
Used for endpoints that handle a single specific operation. The file is created under public/api/standart/.
$ artiframe make:api standart user/update-profile
# Generated file: public/api/standart/user/update-profile.php
Generated file content:
<?php
// Allowed HTTP methods
$allowedMethods = ['POST'];
require_once $_SERVER['DOCUMENT_ROOT'] . '/../app/ApiControl.php';
use Bin\SystemMethod;
// Business Logic Start
// ...
jsonResponse(['status' => 'success', 'message' => 'Operation successful.'], 200);
2. switch-case — Multi-Purpose CRUD Endpoint
Used for endpoints that manage multiple operations (create, update, delete...) within a single file. The file is created under public/api/switch-case/.
$ artiframe make:api switch-case products
# Generated file: public/api/switch-case/products.php
Generated file content:
<?php
$allowedMethods = ['POST'];
require_once $_SERVER['DOCUMENT_ROOT'] . '/../app/ApiControl.php';
use Bin\SystemMethod;
$action = stripHtml($_POST['action'] ?? '');
switch ($action) {
case 'create':
// TODO: Create logic here
jsonResponse(['status' => 'success', 'message' => 'Successfully created.'], 200);
break;
case 'update':
// TODO: Update logic here
jsonResponse(['status' => 'success', 'message' => 'Successfully updated.'], 200);
break;
case 'delete':
// TODO: Delete logic here
jsonResponse(['status' => 'success', 'message' => 'Successfully deleted.'], 200);
break;
default:
jsonResponse(['status' => 'error', 'message' => 'Invalid or missing action parameter.'], 400);
break;
}
How to Call from Frontend?
ArtiFrame APIs typically expect POST methods and return JSON responses. They should be called in accordance with the template in your View JS files:
// switch-case endpoint example
fetch('/api/switch-case/products', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-CSRF-TOKEN': csrfToken
},
body: JSON.stringify({ action: 'create', name: 'Product Name' })
})
.then(res => res.json())
.then(data => console.log(data));
auth/ Special Behavior
If the target path starts with auth (e.g., make:api standart auth/login), the system utilizes either the auth-api-standart.stub or auth-api-switch-case.stub template.
These templates contain the underlying skeleton for session control and authentication business logic.