Documentation · Chapter 03

Add a page
end to end.

A classic page needs an address, an action, and a display file. We will create /about in that order.

You will change one element at a time, in an exact file, then verify the result.

1. Generate the controller

phpaml — zsh
aml make:controller About

AML creates src/controllers/AboutController.php. A controller is an ordinary PHP class extending Controller so it can use framework tools, including view().

2. Fill this exact file

src/controllers/AboutController.php

phpaml — zsh
<?php

declare(strict_types=1);

namespace App\Controllers;

use PHPAML\Http\Request;
use PHPAML\Http\Response;
use PHPAML\Mvc\Controller;

final class AboutController extends Controller
{
    public function index(Request $request): Response
    {
        return $this->view('about.php');
    }
}

index() is the action called by the route. Request represents the received request. Response ensures the action returns an answer PHPAML understands. view('about.php') finally requests src/views/about.php.

3. Create the view

src/views/about.php

phpaml — zsh
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>About</title>
</head>
<body>
    <main>
        <h1>About Reading List</h1>
        <p>My first PHPAML page.</p>
    </main>
</body>
</html>

This view contains HTML because its job is presentation. It remains a PHP file: soon we will display dynamic values in it.

4. Connect the address to the controller

src/routes/WebApp.php

phpaml — zsh
<?php

declare(strict_types=1);

namespace App\Routes;

use App\Controllers\AboutController;
use App\Controllers\HomeController;
use PHPAML\Routing\Route;

final class WebApp extends Route
{
    protected function routes(): void
    {
        $this->get('/', [HomeController::class, 'index']);
        $this->get('/about', [AboutController::class, 'index']);
    }
}

get means the page answers a normal browser visit. The array names the class, then the method to call. The use line makes AboutController available in this file.

5. Check immediately

phpaml — zsh
aml routes
# GET  /about  AboutController::index

Then open http://127.0.0.1:8910/about. If the page appears, the route, controller, and view are correctly connected.