Návod
API
Endpointy cez API providery, jasný JSON kontrakt a pravidlá prístupu v `Api.yaml`.
Registruj endpointy cez providery
Aplikačné API endpointy registruj cez ApiEndpointProviderInterface, nie cez frontend routing. Registrácia tak zostane zladená s OpenAPI aj autorizáciou API.
<?php
declare(strict_types=1);
namespace App\Api;
use Lemonade\Framework\Api\Endpoint\ApiAccess;
use Lemonade\Framework\Api\Endpoint\ApiEndpointProviderInterface;
use Lemonade\Framework\Api\Endpoint\ApiEndpointRegistry;
final class AppApiEndpointProvider implements ApiEndpointProviderInterface
{
public function register(ApiEndpointRegistry $registry): void
{
$registry->get(
'/articles',
'App\\Controllers\\Api\\ArticleController@index',
'articles.index',
'List articles',
'Returns published articles.',
ApiAccess::Public,
);
}
}Vracaj predvídateľný JSON
Tvar response drž explicitný. Vstup validuj ešte pred zostavením payloadu a json() používaj s jasným a stabilným kontraktom.
return $this->json([
'ok' => true,
'data' => [
'id' => $article['id'],
'title' => $article['title'],
],
]);Nastav ochranu endpointu
Pravidlá prístupu k API patria do app/Config/Api.yaml. Pri chránených endpointoch používaj bearer scopes a verejný prístup povoľ iba tam, kde je naozaj zamýšľaný.
module: api
config:
enabled: true
prefix: /api
security:
static_bearer:
enabled: true
token:
$env: API_TOKEN
type: string
scopes:
- api:admin