PHP code example of codesignificant / api-pro

1. Go to this page and download the library: Download codesignificant/api-pro 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/ */

    

codesignificant / api-pro example snippets


#[Controller('/v1/products')]
class ProductController
{
    private ProductService $service;

    public function __construct() {
        $this->service = new ProductService();
    }

    #[Post('/purchase')]
    public function purchase($request)
    {
        // 1. Authenticate and resolve session context
        $session = Session::Get(); 
        $userId = $session['id'];

        // 2. Validate mandatory request body arguments (throws 400 if missing or invalid)
        $productId = $request->body->getString('productId');

        // 3. Delegate to the pure service
        return $this->service->purchaseProduct($userId, $productId);
    }
}

class ProductService
{
    public function purchaseProduct($userId, string $productId): DataResponse
    {
        $safeId = ProSql::Escape($productId);
        
        // Pure business operation decoupled from HTTP variables
        $product = ProSql::FetchItem("SELECT * FROM products WHERE id = '$safeId'")->getData();
        if (!$product) {
            return new DataFailed("Product not found", 404);
        }

        // Process purchase...
        return new DataSuccess("Purchase completed successfully", [
            'productId' => $productId,
            'timestamp' => time()
        ]);
    }
}

#[Post('/user-details')]
public function showDetails($request)
{
    // Access GET/POST query parameters
    $request->params->getString('key');
    
    // Access POST/PATCH/DELETE JSON or form body
    $request->body->getInt('key');

    // Access uploaded files
    $request->multipart->getFile('avatar');
}

#[Post('/book')]
public function bookRoom($request)
{
    $request->addComment("This endpoint accepts a booking. Requires roomId (int) and description (string).");
    ...
}

$lock = "balance_deduct_" . $userId;
if (!ProLock::acquire($lock, 5)) { // Attempt to acquire lock with 5s timeout
    return new DataFailed("System busy. Please try again.", 409);
}

try {
    // Perform critical write action safely...
} finally {
    ProLock::release($lock); // Always release lock in finally block
}

// 1. Default (uses global DATA_ENC key)
return new DataSuccess("Message", $data);

// 2. Explicitly unencrypted (passes empty string key override)
return new DataSuccess("Message", $data, 200, '');

// 3. Custom key encryption (uses 'custom_secret_key')
return new DataSuccess("Message", $data, 200, 'custom_secret_key');
bash
php -S 127.0.0.1:8000 index.php
bash
  php api-pro version
  
bash
  php api-pro update [version]
  
nginx
     location / {
         try_files $uri $uri/ /index.php?$query_string;
     }
     
bash
php api-pro update 2.4.2