Documentation · Chapter 04

Receive values
on the server.

The browser turns fields into a request. PHPAML exposes them through the Request object.

We continue the reading-list project. Data will be kept in the session before introducing a database.

Declare the POST destination

src/routes/WebApp.php

phpaml — zsh
$this->get('/books/create', [BookController::class, 'create']);
$this->post('/books', [BookController::class, 'store']);

The same address can behave differently depending on its method. Here GET /books displays the list and POST /books adds a book.

Read fields without global variables

phpaml — zsh
public function store(Request $request): Response
{
    $title = trim((string) $request->input('title', ''));
    $author = trim((string) $request->input('author', ''));

    return $this->view('books/received.php', [
        'title' => $title,
        'author' => $author,
    ]);
}

input('title', '') looks for the title key and returns an empty string when missing. The string cast normalizes its type; trim removes unnecessary surrounding whitespace. Reading a value does not mean it is valid.

What Request actually contains

method()

GET, POST, PUT, PATCH, or DELETE.

path()

Requested path, such as /books.

query()

Values placed after ? in the address.

input()

Combines address and submitted body values.

Do not use $_POST directly in this project

Request centralizes data access, enables testing, and also handles JSON or encoded bodies. This keeps the controller independent from how PHP received the request.

Test the journey before adding validation

  1. Check GET /books/create and POST /books with aml routes.
  2. Open /books/create and fill both fields.
  3. Submit and confirm the received values.
  4. Do not yet use these values to build SQL or a file name.