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

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

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

Маршрут:

use Medusa\Http\Api;

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

Контроллер:

namespace App\Controllers;

use Medusa\Base\Abstracts\Response as ApiResponse;
use Medusa\Base\Interfaces\Resource;
use Medusa\Http\Request;
use Medusa\Http\Response;

class PhotoController implements Resource
{
    public static function index(Request $request): ApiResponse
    {
        return new Response\JsonResult([
            '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\JsonError('Method Not Allowed', 405);
    }

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

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

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

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

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

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

    public static function destroy(string|int $id): ApiResponse
    {
        return new Response\JsonResult([
            'DELETE /photos/{id}',
            'Method: destroy',
            sprintf('id: #%s', $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": "jpg"
  }
}
{
  "result": [
    "PUT/PATCH /photos/{id}",
    "Method: update",
    "id: #1; Json: {\"name\":\"photo-2\",\"type\":\"jpg\"}"
  ]
}
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"
  ]
}