Custom API endpoints

Wireframe API was designed to be extendable, and thus it's possible to add custom endpoints on a case-by-case basis.

Custom endpoints can be added by using WireframeAPI::addEndpoint(string $endpoint, callable $callable). Each endpoint needs a unique name and a callable: the callable that you provide will receive two arguments (an array containing parts of the path provided after the endpoint name, and an array of arguments passed to the endpoint) and should return an array of values (data):

$api->addEndpoint('home', function($path, $args) use ($pages) {
    return [
        'path' => $path,
        'args' => $args,
        'url' => $pages->get(1)->url
    ];
});

If "callable" is a new concept to you, note that the example above is really just the most basic form. A callable can also be a function name, an array with a class name (or class instance) and a method name, string containing static class method (MyClass::myMethod), and more. See PHP docs for Callbacks / Callables for more details.

Here's an example return value from our newly added "home" endpoint:

curl https://www.yoursite.tld/wireframe-api/home/and/then/some/
{
    "success": true,
    "message": "",
    "path": "wireframe-api\/home\/and\/then\/some",
    "args": [],
    "data": {
        "path": [
            "and",
            "then",
            "some"
        ],
        "args": [],
        "url": "\/"
    }
}

Replacing existing endpoints with custom ones

While this is somewhat less likely need, it's probably worth mentioning that you can remove existing endpoints and replace them with something else. Endpoints can be removed with the WireframeAPI::removeEndpoint(string $endpoint) method, and then replaced with new ones (using the addEndpoint() method we introduced earlier).

Back to top