Encodera

Thanks for reading!

Donate to the site — we work hard for you.

Contents

The response

The response is what the application sends to the browser: the page text, a status code and headers. In Slim it is the $response object, the handler's second argument. The handler adds to it and must return it.

The response body

The body is the page content. It is written with write():

// public/index.php
$app->get('/about', function (Request $request, Response $response) {
    $response->getBody()->write('<h1>About the blog</h1>');
    $response->getBody()->write('<p>The site runs on Slim.</p>');
 
    return $response;
});

The /about address shows a heading and a paragraph: each write() call appends text to the end.

The status code

The status code says how the request ended: 200 is success, 404 is not found, 201 means a record was created. The default is 200. It is changed with withStatus():

// public/index.php
$app->get('/missing', function (Request $request, Response $response) {
    $response->getBody()->write('No such page');
 
    return $response->withStatus(404);
});

The text appears on the page, and in the browser developer tools, on the Network tab, the request shows code 404.

Headers

A response header is added with withHeader():

return $response->withHeader('Content-Type', 'text/plain; charset=utf-8');

Content-Type tells the browser what kind of content it is: with text/plain the text is shown as is, without parsing tags.

A JSON response

JSON is the text data format that APIs answer in. Slim has no ready-made method: the data is turned into a string with json_encode() and a header is added. To avoid repetition, write your own function in public/index.php before the routes:

// public/index.php
function json(Response $response, mixed $data, int $status = 200): Response
{
    $response->getBody()->write(json_encode($data, JSON_UNESCAPED_UNICODE));
 
    return $response
        ->withHeader('Content-Type', 'application/json')
        ->withStatus($status);
}
 
$app->get('/api/status', function (Request $request, Response $response) {
    return json($response, ['status' => 'ok']);
});

At /api/status the browser shows {"status":"ok"}.

A redirect

A redirect is a response without content that sends the browser to another address. It is code 302 plus a Location header:

// public/index.php
$app->get('/old-about', function (Request $request, Response $response) {
    return $response
        ->withHeader('Location', '/about')
        ->withStatus(302);
});

Open /old-about — the browser goes to /about by itself. Code 301 means the page has moved permanently.

Common mistake

PSR-7 objects are immutable: methods with the with prefix do not change the object, they return a new one. A line without an assignment does nothing:

$response->withStatus(404); // the result is lost, the code stays 200
 
return $response;

The result must be stored in a variable or returned right away: return $response->withStatus(404);. The body is the exception: getBody()->write() appends text to the same object.

Thanks for reading!

Donate to the site — we work hard for you.