Руководство
API
Endpoint через API-провайдеры, понятный JSON-контракт и правила доступа в `Api.yaml`.
Регистрируйте endpoint через провайдеры
Регистрируйте API endpoint приложения через ApiEndpointProviderInterface, а не через frontend routing. Так регистрация остаётся согласованной с OpenAPI и авторизацией 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,
);
}
}Возвращайте предсказуемый JSON
Держите структуру response явной. Валидируйте вход до сборки payload и используйте json() с понятным и стабильным контрактом.
return $this->json([
'ok' => true,
'data' => [
'id' => $article['id'],
'title' => $article['title'],
],
]);Защитите endpoint
Правила доступа API относятся к app/Config/Api.yaml. Для защищённых endpoint используйте bearer scopes, а публичный доступ разрешайте только там, где он действительно предусмотрен.
module: api
config:
enabled: true
prefix: /api
security:
static_bearer:
enabled: true
token:
$env: API_TOKEN
type: string
scopes:
- api:admin