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

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

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

Маршрут:

use App\Http\Route;

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

Контроллер:

namespace App\Controllers;

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

class PhotoController implements Resource
{
    public static function index(Request $request): void
    {
        $response = new Response(
            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)
        );

        echo print_r($response->get(), true);
    }

    public static function create(): void
    {
        $response = new Response(
            new Response\Result(['GET /photos/create', 'Method: create'], 200)
        );

        echo print_r($response->get(), true);
    }

    public static function store(Request $request): void
    {
        $payload = $request->get('data', Request::POST);
        $response = new Response(
            new Response\Result([
                'POST /photos',
                'Method: store',
                sprintf('Json: %s', json_encode($payload)),
            ], 200)
        );

        echo print_r($response->get(), true);
    }

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

        echo print_r($response->get(), true);
    }

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

        echo print_r($response->get(), true);
    }

    public static function update(Request $request, int $id): void
    {
        $payload = $request->get('data', Request::PUT) ?? $request->get('data', Request::PATCH);
        $response = new Response(
            new Response\Result([
                'PUT/PATCH /photos/{id}',
                'Method: update',
                sprintf('id: #%d; Json: %s', $id, json_encode($payload)),
            ], 200)
        );

        echo print_r($response->get(), true);
    }

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

        echo print_r($response->get(), true);
    }
}

Результат:

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