The Slim Framework for PHP 5 version 1.6.0 (currently in the develop branch) introduces new middleware that will automatically parse the HTTP request body based on its content type. This middleware will parse HTTP requests with a JSON, XML, or CSV content type. To enable this middleware, do this:
After you add the Slim_Middleware_ContentTypes middleware to your Slim application, the Request object’s body property will be parsed appropriately. However, you may always fetch the raw, unparsed request body in the Slim application’s environment array with key
This middleware will parse a JSON request’s body into an associative array. Imagine you send an HTTP request to the POST route below. The request body is
In this example, the $body variable will equal
array('name' => 'John', 'email' => '[email protected]').
This middleware will parse a XML request’s body into a SimpleXMLElement instance if the SimpleXML extension is available; otherwise the body will remain a string. Imagine you send an HTTP request to the POST route above. The HTTP request body is:
$body variable will be an instance of SimpleXMLElement; you can fetch the POSTed book properties with
This middleware will parse a CSV request’s body into an array or arrays. Imagine you send an HTTP request to the POST route above. The HTTP request body is:
$body variable will be this array:
Custom Content Types
If you want to parse an HTTP request body that is not JSON, XML, or CSV you can provide your own parsing functions to this middleware using its optional second argument. You’ll need to know the content type of the incoming HTTP request, and you’ll need to prepare a callable item to parse the request body. The callable item that will parse the HTTP request body should accept a string argument and return the appropriate PHP data structure.
Imagine we want the POST route above to accept an HTTP request with an HTML body. We tell our Slim_Middleware_ContentTypes middleware to parse these requests like this:
Assuming we continue using the POST route defined above, the
$body variable will become a DOMDocument instance representative of the HTML provided in the HTTP request body. The custom parsing function you define will be merged with the default parsing functions (keyed by content-type). In this example, the Slim_Middleware_ContentTypes middleware will now parse HTTP requests that have a JSON, XML, CSV, or HTML content type.
If you want to override the default parsing function for JSON requests, define a new parser for content type
application/json; your own parser will then be used instead of the default parser.