Folders and index.json, the menu, one template for several pages, parts, and the request.
- **You will have:** a Services section in the menu: an overview listing its pages, two pages drawn by one
template, a price computed from the url, a 422 for a wrong value, and their tests.
- **You need:** **Your first project** done.
## 1. A folder is a section
The pages are not registered in code. The theme reads every `index.json` under `src/pages`: its folder is a url,
and the files it names are the pages under it. Create `src/pages/services/index.json`:
```json from=src/pages/services/index.json
{
"label": "Services",
"icon": "briefcase",
"group": "Acme Portal",
"order": 5,
//`services` alone serves its overview
"default": "overview",
```
| Key | What it does |
|---|---|
| `label` | the folder's name in the menu |
| `icon` | an icon name, or a character |
| `group` | the section of the menu it is filed under |
| `order` | sorts it among the others |
| `default` | the page served at the folder's own url, `services` |
Without a `default`, `services` would show a listing of the folder's pages.
## 2. Its pages
Below, `files` names the pages. Each key is a url segment, and renders `<key>.phtml` beside the `index.json`:
```json from=src/pages/services/index.json
"files": {
"overview": {
"label": "Overview",
"title": "Our services",
"description": "What Acme does for its customers."
},
```
`title` is the page's `<title>` and heading; `description` its meta description. Give them here, not in the
template: the controller writes them after the page has run.
The overview lists the other pages of its folder. It reads them from the page tree rather than writing them a
second time, in `src/pages/services/overview.phtml`:
```php from=src/pages/services/overview.phtml
$content = $_VARS['controller']->getMainContent();
?>
<h1><?= htmlspecialchars($content->getTitle()) ?></h1>
<ul>
<?php foreach ($content->getParent()->getChildren() as $page): ?>
<?php if ($page === $content) {continue;} ?>
<li><a href="<?= htmlspecialchars($page->url()) ?>"><?= htmlspecialchars($page->getLabel()) ?></a></li>
<?php endforeach; ?>
</ul>
```
A page is plain php, run with two things in scope: `$this`, the html document being built, and `$_VARS`, its
variables. `$_VARS['route']` is the request, `$_VARS['controller']` the PageController serving it. They are not
separate variables: write `$_VARS['route']`, never `$route`.
## 3. One template, several pages
Consulting and Support are the same page with other values. Name the template with `file`, and put what differs
in `data`:
```json from=src/pages/services/index.json
"consulting": {
"label": "Consulting",
"title": "Consulting",
"description": "An Acme engineer, on your project.",
"file": "service.phtml",
"data": {"rate": 900, "unit": "day"}
},
```
`support` is the same with `"rate": 300, "unit": "month"`. In the template, each key of `data` is in `$_VARS`:
`$_VARS['rate']`. Keep `data` to names of your own: a key named `route`, `controller`, `document` or `app` would
hide the one the page needs.
## 4. Reading the request, answering an error
`src/pages/services/service.phtml` reads the quantity from the url, `services/consulting?quantity=3`:
```php from=src/pages/services/service.phtml
$route = $_VARS['route'];
$content = $_VARS['controller']->getMainContent();
$quantity = (int)$route->get('quantity', 1);
```
A wrong value is answered with its status. Throwing an exception in a page does not do that: the page catches
it and prints it in its place, with a 200. Change the response's status instead, then `return` to end the page:
```php from=src/pages/services/service.phtml
<?php if ($quantity < 1): ?>
<?php $route->setResponse($route->getResponse()->withStatus(422)); ?>
<p class="error-message">The quantity must be 1 or more.</p>
<?php return; ?>
<?php endif; ?>
```
A url that no page matches, `services/training`, is a 404 already: the controller answers it before any page
runs.
## 5. Parts
The price is a part: a `.phtml` that no `index.json` names, so it is no page, imported by the pages that need it.
`src/pages/services/parts/price.phtml`:
```php from=src/pages/services/parts/price.phtml
$total = $_VARS['rate'] * $_VARS['quantity'];
?>
<p class="price"><?= $_VARS['quantity'] ?> <?= htmlspecialchars($_VARS['unit']) ?>(s) × <?= number_format($_VARS['rate']) ?> € = <strong><?= number_format($total) ?> €</strong></p>
```
The service page imports it with its own variables:
```php from=src/pages/services/service.phtml
<?= $this->import('services/parts/price.phtml', ['rate' => $_VARS['rate'], 'unit' => $_VARS['unit'], 'quantity' => $quantity], false) ?>
```
Two things to know about `import()`:
- **The name is relative to the pages folder**, not to the calling file: `services/parts/price.phtml` from any page.
- **`false` returns the html**, which the page echoes where it stands. Without it, the part is added to the end of
the document, below the whole page.
## 6. Try it, then test it
<http://localhost:8000/services>: the menu has a Services category with its three pages, and the overview links to
the other two. <http://localhost:8000/services/consulting?quantity=3> shows `3 day(s) × 900 € = 2,700 €`.
`tests/unit/TestServices.php`:
```php from=tests/unit/TestServices.php
public function testOneTemplateServesBothPagesWithTheirData(): void {
$this->assertStringContainsString('<strong>2,700 €</strong>', (string)$this->http('GET', 'services/consulting?quantity=3')->getBody());
$this->assertStringContainsString('<strong>300 €</strong>', (string)$this->http('GET', 'services/support')->getBody());
}
public function testAWrongQuantityIs422AndAnUnknownPage404(): void {
$this->assertSame(422, $this->http('GET', 'services/support?quantity=0')->getStatusCode());
$this->assertSame(404, $this->http('GET', 'services/training')->getStatusCode());
}
```
```bash
phpunit
```
## Common mistakes
- **`<?= $value ?>` with a value from outside the file.** A page prints raw html: escape text with
`htmlspecialchars()`.
- **`setTitle()` in a page.** The controller writes the title after the page: give `title` in `index.json`.
- **`throw new e404(...)` in a page.** It shows the error inside a 200. Set the status on the response.
- **`$this->import('parts/price.phtml')` without `false`.** The part lands below the page, not where it is called.
- **`'../parts/price.phtml'`.** A part's name is relative to the pages folder, whatever page calls it.
## Going further
- Next: **Your first controller**, for what a page should not do: an API.
- [Pages](https://mmoframework.fr/en/documentation/mmo.client/Pages.md): every key of `index.json`, the menu,
the shell's regions and layouts, forms posting to their page, error pages.