Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/aidan-casey/http-parser
Various tools for parsing HTTP requests into API usable queries.
https://github.com/aidan-casey/http-parser
api database psr-7 slimframework zend-db zend-framework
Last synced: 13 days ago
JSON representation
Various tools for parsing HTTP requests into API usable queries.
- Host: GitHub
- URL: https://github.com/aidan-casey/http-parser
- Owner: aidan-casey
- License: mit
- Created: 2018-09-03T21:25:20.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2018-09-10T14:54:04.000Z (over 6 years ago)
- Last Synced: 2024-11-18T06:02:00.485Z (3 months ago)
- Topics: api, database, psr-7, slimframework, zend-db, zend-framework
- Language: PHP
- Size: 19.5 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
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
|Parameter|Request Method|Value|
|--|--|--|
|fields|SELECT|String|
|filter|DELETE, SELECT, PATCH, PUT|JSON|
|orderby|SELECT|JSON|
|limit|SELECT|Integer|
|offset|SELECT|Integer|### Fields
```fields=ID, Name```### Filter
```json
filter={
"ID": {
"$gte" : 4
},
"Name": "Example"
}
```#### Supported Filters
|Name|Key|Value|
|--|--|--|
|Greater Than|$gt|Integer|
|Greater Than or Equal To|$gte|Integer|
|In|$in|Array|
|Not In|$nin|Array|
|Like|$like|String|
|Not Like|$nlike|String|
|Less Than|$lt|Integer|
|Less Than or Equal To|$lte|Integer|
|Not|$not|String|### Order By
```json
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
[
'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();
```