PHP code example of aero / aeroapi

1. Go to this page and download the library: Download aero/aeroapi library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

aero / aeroapi example snippets




namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    protected $table = 'users';
    protected $fillable = ['name', 'email', 'password'];
    public $timestamps = false;
}

use App\Models\User;

// 1. Ambil semua Data
$users = User::all();

// 2. Insert Data Baru
$user = User::create([
    'name' => 'Budi Utomo',
    'email' => '[email protected]',
    'password' => password_hash('password123', PASSWORD_BCRYPT)
]);

// 3. Update Data
$user = User::find(1);
if ($user) {
    $user->name = 'Budi Ganteng';
    $user->save();
}

// 4. Hapus Data
User::destroy(2);

use Core\Route;
use App\Controllers\HomeController;
use App\Controllers\ApiController;

// Standard Web Routing
Route::get('/', [HomeController::class, 'index']);
Route::post('/api/submit', [HomeController::class, 'submitData']);

// RESTful HTTP API Routing (GET, POST, PUT, DELETE)
Route::get('/api/items', [ApiController::class, 'getItems']);
Route::get('/api/items/detail', [ApiController::class, 'getItemDetail']);
Route::post('/api/items', [ApiController::class, 'createItem']);
Route::put('/api/items', [ApiController::class, 'updateItem']);
Route::delete('/api/items', [ApiController::class, 'deleteItem']);

use Core\Route;
use App\Controllers\UserController;
use App\Middlewares\AuthMiddleware;

// Semua route di dalam grup ini otomatis memiliki prefix /api dan melewati AuthMiddleware
Route::group(['prefix' => '/api', 'middleware' => AuthMiddleware::class], function() {
    
    // URL: /api/users
    Route::get('/users', [UserController::class, 'index']);
    
    // URL: /api/profile
    Route::get('/profile', [UserController::class, 'profile']);
});



namespace App\Services;

use App\Models\User;

class UserService
{
    public function getActiveUsersCount(): int
    {
        return User::count();
    }
}

namespace App\Controllers;

use Core\Controller;
use App\Services\UserService;

class UserController extends Controller
{
    protected UserService $userService;

    // UserService otomatis di-inject di sini!
    public function __construct(UserService $userService)
    {
        $this->userService = $userService;
    }

    public function index()
    {
        $count = $this->userService->getActiveUsersCount();
        return $this->json(['active_users' => $count]);
    }
}

use Core\Route;
use App\Middlewares\AuthMiddleware;

Route::get('/api/users', [\App\Controllers\UserController::class, 'index'])
    ->middleware(AuthMiddleware::class);

class UserController extends Controller
{
    public function __construct()
    {
        // Hanya jalankan AuthMiddleware pada action 'index'
        $this->registerMiddleware(new AuthMiddleware(['index']));
    }
}



return [
    'name' => 'REST POST - Tambah Item Baru',
    'url' => 'http://localhost:8000/api/items',
    'method' => 'POST',
    'headers' => [
        'Accept' => 'application/json',
        'Content-Type' => 'application/json',
    ],
    'body' => json_encode([
        'title' => 'Judul Baru',
        'body' => 'Isi item baru dari benchmark.'
    ]),
    'requests' => 15, // Jumlah request stres-tes yang dikirim
];
bash
# A. Buat struktur tabel database bawaan
php aero migrate

# B. Isi tabel dengan data seeder awal (opsional)
php aero seed
bash
# Server akan aktif di http://localhost:8000
php aero serve
bash
# A. Memunculkan Menu Pilihan Interaktif berkas pengujian (0-7)
php aero benchmark

# B. Menjalankan secara langsung berkas pengujian nomor indeks 3 (bypass menu)
php aero benchmark 3

# C. Menjalankan berkas pengujian spesifik berdasarkan nama berkas
php aero benchmark benchmark_users.php