Handlers, not presenters
Every endpoint is a small class with params(), outputs() and handle(). ApiDecider maps method, version and package to it.
Add a complete API layer to Nette: versioned endpoints, authorization, validated params and outputs, CORS, rate limiting, logging and OpenAPI docs – wired through Nette DI, one handler class per endpoint.
A handler is one class:
<?php
declare(strict_types=1);
namespace App\Api;
use Tomaj\NetteApi\Handlers\BaseHandler;
use Tomaj\NetteApi\Params\GetInputParam;
use Tomaj\NetteApi\Response\JsonApiResponse;
use Tomaj\NetteApi\Response\ResponseInterface;
final class HelloHandler extends BaseHandler
{
public function summary(): string
{
return 'Say hello';
}
public function params(): array
{
return [
(new GetInputParam('name'))->setDefault('world')->setDescription('Who to greet'),
];
}
public function handle(array $params): ResponseInterface
{
return new JsonApiResponse(200, ['message' => 'Hello ' . $params['name'] . '!']);
}
}and a few lines of configuration register it as GET /api/v1/hello:
application:
mapping:
# Api:Api:default is Tomaj\NetteApi\Presenters\ApiPresenter
Api: Tomaj\NetteApi\Presenters\*Presenter
services:
router: App\RouterFactory::create
# Nette 3.3 creates presenters only from the DI container
apiPresenter:
factory: Tomaj\NetteApi\Presenters\ApiPresenter
# required by ApiPresenter: when to validate output schemas and show error details
- Tomaj\NetteApi\Output\Configurator\DebuggerConfigurator
# required by ApiPresenter: turns errors into JSON responses
- Tomaj\NetteApi\Error\DefaultErrorHandler
apiDecider:
factory: Tomaj\NetteApi\ApiDecider
setup:
# GET /api/v1/hello
- addApi(
Tomaj\NetteApi\EndpointIdentifier('GET', 1, 'hello'),
App\Api\HelloHandler(),
Tomaj\NetteApi\Authorization\NoAuthorization()
)The hello world example runs exactly this code in CI.
A library that adds a REST API layer to a Nette application: endpoints, validated params and outputs, authorization, CORS, rate limits, logging and an OpenAPI document. See Getting started.
Through one route to its ApiPresenter and services registered in your neon config, so handlers get their dependencies from Nette DI like any other service. See Getting started.
PHP 8.3 or newer, nette/application and nette/http 3.x, and Latte 3. The full list is in Getting started.
No. The library ships its own ApiPresenter, and your code lives in handlers. See Handlers.
The version is part of every endpoint, so v1 and v2 of the same URL can point to different handlers. See Links and versions and the versions example.
Give it an authorization when you register it: bearer token, API key in a header, query or cookie, or your own class. See Authorization.
Handlers registered by class name are created from the DI container only when their endpoint is called. The exception is the OpenAPI, listing and console endpoints: they read every endpoint through getApis(), which creates all handlers. See Architecture.
Yes. Register OpenApiHandler and it describes every endpoint as OpenAPI 3.0, in JSON or YAML. See OpenAPI.
Yes, every page has a Slovak version. Use the language menu at the top, or start at the Slovak home page.
There is no 4.0 yet; 3.x is the current line. A 4.0.0 milestone is open with planned breaking changes, such as removing the unsafe 'auto' CORS mode and raising the minimum PHP version. It has no release date yet. The upgrade guide covers older major versions.