The request
Everything the browser sent — the address, parameters, form data, headers — Slim gathers into one request object. A handler receives it as the first argument, $request. The object follows PSR-7, a shared PHP standard that describes the request and the response, so the same methods appear in other frameworks too.
The $_GET and $_POST superglobals are not used in Slim: all data is read from $request.
Query parameters
Parameters after the question mark are returned by getQueryParams():
// public/index.php
$app->get('/search', function (Request $request, Response $response) {
$params = $request->getQueryParams();
$query = $params['q'] ?? '';
$response->getBody()->write('Searching for: ' . htmlspecialchars($query));
return $response;
});Open http://localhost:8080/search?q=slim — the page shows "Searching for: slim". The method returns a plain array; when the parameter is missing, the value after ?? is used. The htmlspecialchars() function neutralises HTML in text you did not write.
Form data and JSON
The request body — form data or JSON — is returned by getParsedBody(). To make Slim parse JSON as well, add one line to public/index.php after creating the application:
// public/index.php
$app = AppFactory::create();
$app->addBodyParsingMiddleware();Now the data is read the same way in both cases:
// public/index.php
$app->post('/posts', function (Request $request, Response $response) {
$data = (array) $request->getParsedBody();
$title = trim($data['title'] ?? '');
$response->getBody()->write('Post created: ' . htmlspecialchars($title));
return $response;
});Try it from the terminal:
curl -X POST -d "title=First post" http://localhost:8080/postsThe answer is "Post created: First post". The (array) cast is needed because the method returns null for an empty body.
Headers
A header is a service line of the request in the form "name: value". It is read with getHeaderLine():
$agent = $request->getHeaderLine('User-Agent');If the header is missing, an empty string is returned. The letter case of the name does not matter.
What else the request holds
| Method | What it returns |
|---|---|
getMethod() | The HTTP method: GET, POST |
getUri()->getPath() | The address path: /posts/5 |
hasHeader('Authorization') | Whether the header is present |
getCookieParams() | The cookie array |
getServerParams() | Server data, for example REMOTE_ADDR |
getUploadedFiles() | Uploaded files |
getAttribute('user') | A request attribute |
Attributes
Attributes are data added to the request not by the browser but by the application itself on the way to the handler. For example, a middleware checks the login and puts the user into the request, and the handler reads it:
$user = $request->getAttribute('user');If the attribute is missing, null is returned. How a middleware adds an attribute is shown in the middleware lesson.
Common mistake
getParsedBody() returns null for JSON if you forget $app->addBodyParsingMiddleware() or if the client does not send the Content-Type: application/json header. PHP parses ordinary form data by itself, so the error shows up only on JSON requests — and takes a long time to find.