ZealAPI — File-Based REST

Drop a PHP file in api/ and it becomes an endpoint automatically. The file defines a closure named after the HTTP method. $this inside the closure is the ZealAPI instance.

api/users/get.php → GET /api/users/get
<?php
// File: api/users/get.php
// Endpoint: GET /api/users/get
// The variable name MUST match basename($file, '.php') → 'get'

use ZealPHP\G;

$get = function() {
    $g = G::instance();
    return [
        'users'  => [['id' => 1, 'name' => 'Alice'], ['id' => 2, 'name' => 'Bob']],
        'method' => $g->server['REQUEST_METHOD'],
        'query'  => $g->get,
    ];
};

Routing convention

FileVariableEndpoint
api/users/get.php$get = function(){}GET /api/users/get
api/orders/create.php$create = function(){}POST /api/orders/create
api/device/list.php$list = function(){}GET /api/device/list
api/php/sapi_name.php$sapi_name = function(){}GET /api/php/sapi_name

Live ZealAPI endpoints

GET GET /api/php/sapi_name — returns SAPI name
// api/php/sapi_name.php
$sapi_name = function() {
    return ['sapi' => php_sapi_name(), 'async' => true];
};
LIVE OUTPUT Click Run →
GET GET /api/php/get — dump GET params
// api/php/get.php
$get = function() {
    $g = G::instance();
    return ['query_params' => $g->get, 'async' => php_sapi_name() === 'cli'];
};
LIVE OUTPUT Click Run →
$this in closures — ZealAPI uses Closure::bind() to set $this to the ZealAPI instance. This gives you access to $this->_request, $this->_response, and helper methods like $this->paramsExists(['id', 'name']).