aidan-casey / http-parser
Various tools for parsing HTTP requests into API usable queries.
v1.2.1
2018-09-10 14:53 UTC
Requires
- psr/http-message: ^1.0
- zendframework/zend-db: ^2.9
Requires (Dev)
- slim/slim: ^3.10
This package is not auto-updated.
Last update: 2024-11-06 16:52:33 UTC
README
This package provides various classes that help convert a PSR representation of an HTTP request into other forms useful for an API server.
For example, the HttpToZend
class will convert HTTP parameters into a database query that can be passed to your model for serving up information or carrying out actions.
Install
Coming soon! For now, feel free to clone this repository.
HttpToZend
Supported GET Parameters / request methods
Fields
fields=ID, Name
Filter
filter={ "ID": { "$gte" : 4 }, "Name": "Example" }
Supported Filters
Order By
orderby={ "Name": "ASC", "ID": "DESC" }
Limit
limit=20
Offset
offset=20
Example
This example uses Slim Framework and converts a GET request into a database select query.
<?php // Provides composer dependencies. require_once 'vendor/autoload.php'; // Start our Slim app. $App = new Slim\App([ 'settings' => [ 'displayErrorDetails' => true ] ]); // Add a database connection to our application container. $Container = $App->getContainer(); $Container['DB'] = function ( $Container ) { // Setup our query adapter. $Adapter = new Zend\Db\Adapter\Adapter([ 'database' => 'Example', 'driver' => 'Mysqli', 'host' => 'localhost', 'password' => '', 'username' => 'ExampleUser' ]); // Now setup our sql object. return new Zend\Db\Sql\Sql( $Adapter ); }; // Setup our API route. $App->get('/', function ( $Request, $Response ) use ( $Container ) { // Parse any filters, etc. $Parser = new AidanCasey\HttpParser\HttpToZend(); $Select = $Parser->ConvertHttpRequest( $Request ); // Prepare a query for the database. $Statement = $Container['DB']->prepareStatementForSqlObject( $Select->from('ExampleTable') ); // Now execute that query. $Result = $Statement->execute(); }); // Run our Slim app. $App->run();