MilkCore

API
in package

API Routing and Management Class

Simplified API system that works like the Route class but for API endpoints. Uses only the "page" parameter, supports slashes in page names, and provides authentication and HTTP method validation.

Tags
example
// Register simple endpoints
API::set('users/list', function($request) {
    return ['users' => User::all()];
});

// With authentication and specific HTTP method
API::set('users/create', function($request) {
    $data = $request['body'];
    return ['user' => User::create($data)];
}, ['auth' => true, 'method' => 'POST']);

// With controller
API::set('users/show', 'UserController@show', ['auth' => true]);

Table of Contents

Methods

error_response()  : void
Send error response
generate_token()  : array<string|int, mixed>
Generate a new JWT token for a user
group()  : void
Group endpoints with common options
json_response()  : void
Send JSON response
list_endpoints()  : array<string|int, mixed>
List all registered endpoints (useful for debugging)
payload()  : array<string|int, mixed>|null
Get the current JWT payload
refresh_token()  : array<string|int, mixed>
Refresh a JWT token
request()  : array<string|int, mixed>|null
Get the current request data
run()  : bool
Execute an API endpoint (like Route::run)
set()  : void
Register an API endpoint
user()  : object|null
Get the current authenticated user

Methods

error_response()

Send error response

public static error_response(string $message[, int $status = 400 ]) : void
Parameters
$message : string

Error message

$status : int = 400

HTTP status code

generate_token()

Generate a new JWT token for a user

public static generate_token(int $user_id[, array<string|int, mixed> $additional_data = [] ]) : array<string|int, mixed>
Parameters
$user_id : int

User ID

$additional_data : array<string|int, mixed> = []

Additional data to include in the token

Return values
array<string|int, mixed>

Token response

group()

Group endpoints with common options

public static group(array<string|int, mixed> $options, callable $callback) : void
Parameters
$options : array<string|int, mixed>

Group options (prefix, auth, permissions, etc.)

$callback : callable

Function containing endpoint registrations

json_response()

Send JSON response

public static json_response(mixed $data[, int $status = 200 ]) : void
Parameters
$data : mixed

Response data

$status : int = 200

HTTP status code

list_endpoints()

List all registered endpoints (useful for debugging)

public static list_endpoints() : array<string|int, mixed>
Return values
array<string|int, mixed>

All registered endpoints

payload()

Get the current JWT payload

public static payload() : array<string|int, mixed>|null
Return values
array<string|int, mixed>|null

The JWT payload or null

refresh_token()

Refresh a JWT token

public static refresh_token() : array<string|int, mixed>
Return values
array<string|int, mixed>

Token response

request()

Get the current request data

public static request() : array<string|int, mixed>|null
Return values
array<string|int, mixed>|null

The current request or null

run()

Execute an API endpoint (like Route::run)

public static run(string $page) : bool
Parameters
$page : string

The requested page

Return values
bool

True if endpoint was found and executed, false otherwise

set()

Register an API endpoint

public static set(string $page, callable|string $handler[, array<string|int, mixed> $options = [] ]) : void
Parameters
$page : string

The endpoint page (e.g., 'users/list', 'auth/login')

$handler : callable|string

The handler function or 'Controller@method' string

$options : array<string|int, mixed> = []

Additional options (auth, method, permissions, etc.)

user()

Get the current authenticated user

public static user() : object|null
Return values
object|null

The authenticated user or null


        
On this page

Search results