Skip to content

PHP library for building fast and modern web APIs.

Notifications You must be signed in to change notification settings

storinka/invoke

Repository files navigation

Invoke

PHP library for building fast and modern web APIs.

Installation

The library is still work-in-progress.

composer require storinka/invoke:^2 storinka/invoke-http:^2

Basic example

  1. Create index.php
use Invoke\Invoke;

function add(float $a, float $b): float
{
    return $a + $b;
}

Invoke::create([
    "add"
])->serve();
  1. Run a server
php -S localhost:8000 index.php 
  1. Make a request
curl 'localhost:8000/add?a=2&b=2'

# { "result": 4 }

Complex example

  1. Create a type
use Invoke\Data;

class UserResult extends Data
{
    public int $id;
    
    public string $name;
}
  1. Create a method to get list of users
use Invoke\Method;

class GetUsers extends Method
{
    protected function handle(int $page, int $perPage): array
    {
        $usersFromDB = getUsersFromDb($page, $perPage);
        
        return UserResult::many($usersFromDB);
    }
}
  1. Setup Invoke
use Invoke\Invoke;

Invoke::create([
    "getUsers" => GetUsers::class
])->serve();
  1. Run a server and try to invoke:
curl 'localhost:8000/getUsers?page=1&perPage=10'

# { "result": [ ... ] }