К содержанию
Meduza
Документация

Ресурсная API-маршрутизация

Метод Api::resource() регистрирует набор CRUD-маршрутов для одного контроллера.

Маршрут:

use App\Http\Api;

Api::resource('photos', App\Controllers\PhotoController::class);

Контроллер:

namespace App\Controllers;

use App\Base\Interfaces\Resource;
use App\Base\Interfaces\Response as ApiResponse;
use App\Http\Request;
use App\Http\Response;

class PhotoController implements Resource
{
    public static function index(Request $request): ApiResponse
    {
        return new Response\Result([
            'GET /photos',
            'Method: index',
            sprintf(
                'Query params: limit %d; offset %d',
                $request->get('limit', Request::GET),
                $request->get('offset', Request::GET)
            ),
        ], 200);
    }

    public static function create(): ApiResponse
    {
        return new Response\Error('Method Not Allowed', 405);
    }

    public static function store(Request $request): ApiResponse
    {
        $payload = $request->get('data', Request::POST);

        return new Response\Result([
            'POST /photos',
            'Method: store',
            sprintf('Json: %s', json_encode($payload)),
        ], 200);
    }

    public static function show(int $id): ApiResponse
    {
        return new Response\Result([
            'GET /photos/{id}',
            'Method: show',
            sprintf('id: #%d', $id),
        ], 200);
    }

    public static function edit(int $id): ApiResponse
    {
        return new Response\Error('Method Not Allowed', 405);
    }

    public static function update(Request $request, int $id): ApiResponse
    {
        $payload = $request->get('data', Request::PUT) ?? $request->get('data', Request::PATCH);

        return new Response\Result([
            'PUT/PATCH /photos/{id}',
            'Method: update',
            sprintf('id: #%d; Json: %s', $id, json_encode($payload)),
        ], 200);
    }

    public static function destroy(int $id): ApiResponse
    {
        return new Response\Result([
            'DELETE /photos/{id}',
            'Method: destroy',
            sprintf('id: #%d', $id),
        ], 200);
    }
}

Результат:

Запрос Ответ
GET /api/photos?limit=1&offset=0 HTTP/1.1
Host: localhost:8001
{
  "result": [
    "GET /photos",
    "Method: index",
    "Query params: limit 1; offset 0"
  ]
}
GET /api/photos/create HTTP/1.1
Host: localhost:8001
{
  "error": {
    "code": 405,
    "message": "Method not allowed"
  }
}
POST /api/photos HTTP/1.1
Host: localhost:8001
Content-Type: application/json
Content-Length: 60
{
  "data": {
    "name": "photo-1",
    "type": "jpg"
  }
}
{
  "result": [
    "POST /photos",
    "Method: store",
    "Json: {\"name\":\"photo-1\",\"type\":\"jpg\"}"
  ]
}
GET /api/photos/1 HTTP/1.1
Host: localhost:8001
{
  "result": [
    "GET /photos/{id}",
    "Method: show",
    "id: #1"
  ]
}
GET /api/photos/1/edit HTTP/1.1
Host: localhost:8001
{
  "error": {
    "code": 405,
    "message": "Method not allowed"
  }
}
PUT /api/photos/1 HTTP/1.1
Host: localhost:8001
Content-Type: application/json
Content-Length: 60
{
  "data": {
    "name": "photo-2",
    "type": "png"
  }
}
{
  "result": [
    "PUT/PATCH /photos/{id}",
    "Method: update",
    "id: #1; Json: {\"name\":\"photo-2\",\"type\":\"png\"}"
  ]
}
PATCH /api/photos/1 HTTP/1.1
Host: localhost:8001
Content-Type: application/json
Content-Length: 41
{
  "data": {
    "name": "photo-3"
  }
}
{
  "result": [
    "PUT/PATCH /photos/{id}",
    "Method: update",
    "id: #1; Json: {\"name\":\"photo-3\"}"
  ]
}
DELETE /api/photos/1 HTTP/1.1
Host: localhost:8001
{
  "result": [
    "DELETE /photos/{id}",
    "Method: destroy",
    "id: #1"
  ]
}