Ada wants to see what the shop sells. Start with a hard-coded catalog — no database yet — so you learn how an API “endpoint” maps to PHP in Pionia.

What is a service? (in general)

In many APIs you write controllers and many URL routes (GET /products, POST /products, …).

Pionia’s Moonlight style is different but the idea is the same: a place for business behavior.

IdeaIn Pionia Shop
A serviceA PHP class for one area of the shop (ProductService, later OrderService)
An actionOne operation clients ask for (list, create, place)
The switchA registry: "product" → ProductService

Clients always POST to /api/v1/ with JSON like:

Code examplejson
{ "service": "product", "action": "list" }

Pionia looks up product, calls listAction, and returns a JSON envelope. You are not inventing a new URL for every button.

What you will learn

  • Scaffold ProductService
  • Map listAction to "action": "list"
  • Register the product alias on MainSwitch
Before you start

Generate ProductService

Terminalbash
php pionia make:service Product

Choose Basic when prompted. Replace the generated listAction in services/ProductService.php; keep its existing Arrayable and ApiResponse imports.

Code examplephp
protected function listAction(Arrayable $data): ApiResponse
{
    $products = [
        ['id' => 1, 'name' => 'Ada Mug', 'price' => 24.50, 'stock' => 12],
        ['id' => 2, 'name' => 'Pionia Sticker Pack', 'price' => 6.00, 'stock' => 200],
    ];

    return response(0, 'OK', ['products' => $products]);
}

In switches/MainSwitch.php, add the import above the class:

Code examplephp
use Application\Services\ProductService;

Add the alias to the array returned by registerServices():

Code examplephp
'product' => ProductService::class,

Try the catalog

Terminalbash
curl -s -X POST http://127.0.0.1:8000/api/v1/ \
  -H "Content-Type: application/json" \
  -d '{"service":"product","action":"list"}'

You should see both products in returnData. Later steps replace the array with table('products').

Common mistakes

  • Forgetting to register product on the switch — you get “service not found”
  • Naming the method list instead of listAction