Step 3 — Your first service
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.
| Idea | In Pionia Shop |
|---|---|
| A service | A PHP class for one area of the shop (ProductService, later OrderService) |
| An action | One operation clients ask for (list, create, place) |
| The switch | A registry: "product" → ProductService |
Clients always POST to /api/v1/ with JSON like:
{ "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
listActionto"action": "list" - Register the
productalias onMainSwitch
- Step 2 — server running
Generate ProductService
php pionia make:service ProductEdit services/ProductService.php:
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]);
}Register on switches/MainSwitch.php:
'product' => ProductService::class,Try the catalog
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
producton the switch — you get “service not found” - Naming the method
listinstead oflistAction