Receiving input into a Slim 4 application
A Slim 4 (and Slim 3) application receives data from three places:
- Any query parameters on the URL (the key-value pairs after the ?)
- The HTTP message's body (usually for POST and PUT) messages
- Parameters in the URL (such as the 3 in https://example.com/users/3
Within the application, these are available within the PSR-7 Request object.
Let's start with a simple Slim 4 application.
Firstly we require the Slim framework and a PSR-7 implementation:
$ composer require slim/slim slim/psr7
Now we write public/index.php:
<?php declare(strict_types=1); use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ServerRequestInterface as Request; use Slim\Factory\AppFactory; require __DIR__ . '/../vendor/autoload.php'; $app = AppFactory::create(); $app->addRoutingMiddleware(); $app->addErrorMiddleware(true, true, true); $app->get('/', function (Request $request, Response $response, $args): Response { // our code for this handler goes here return $response; }); $app->run();
This is the standard minimal Slim 4 application, which will respond solely to the / URL (i.e. the home page of the website) and will return an empty response. See A first look at Slim 4 for more information.
We can run our website with:
$ php -S 0.0.0.0:8888 -t public/
If we navigate to https://localhost:8888 we'll see a blank page.
Query parameters
Query parameters are the key value pairs after the ? in the URL. For example in the ULR http://localhost:8888?name=Rob, we have a parameter name with the value Rob.
To access this in our Slim 4 handler, we use the Request's getQueryParams() method. This will return an array of all query parameter.
i.e. if we add:
// our code for this handler goes here $params = $request->getQueryParams() var_dump($params);
And then navigate to localhost:8888/?name=Rob&country=UK, we will see:
array (size=1) 'name' => string 'Rob' (length=3) 'country' => string 'UK' (length=2)
Note that the PSR-7 RequestInterface doesn't have a method to retrieve a single parameter, so if you just want a one parameter, the easiest way is to use the null coalescing operator (??) like this:
$name = $request->getQueryParameters()['name'] ?? '';
This will either set $name to the value of the name query parameter if it exists or set it to an empty string otherwise
Posted form data
For form data posted to the website from a browser, we can use the $request's getParsedBody() method. This will return an array of the posted data.
Firstly we set up a form:
$app->get('/', function (Request $request, Response $response, $args): Response { $html = <<<html <html> <form method="POST"> <label>Name: <input name="name"></label> <label>Country: <input name="country"></label> <input type="submit"> </form> </html> html; $response->getBody()->write($html); return $response; });
We also need a handler to receive the POSTed data:
$app->post('/', function (Request $request, Response $response, $args): Response { $data = $request->getParsedBody(); $html = var_export($data, true); $response->getBody()->write($html); return $response; });
If we fill out our form with the same name and country as before:
(html/CSS isn't really my forté!)
we'll get:
array ( 'name' => 'Rob', 'country' => 'UK', )
Again, the PSR-7 Request object doesn't have a method to retrieve a single parameter from POSTed data, but in my experience this is rarely a requirement anyway.
POSTed JSON or XML data
It's very common in web APIs to send data in XML or JSON format. Out of the box, PSR-7 implementations do not support these formats, you have to decode the Request object's getBody() yourself. As this is a common requirement, Slim 4 provides BodyParsingMiddleware to handle this task.
This is added using:
$app->addBodyParsing
Truncated by Planet PHP, read more at the original (another 3313 bytes)