pixaye / chiven
Simple library which allows you to build RESTful API fast, easy and flexible
Requires
- php: ^7.1
- ext-json: *
Requires (Dev)
- ext-xdebug: *
- phpunit/phpunit: ^7.5
This package is auto-updated.
Last update: 2025-03-29 00:37:11 UTC
README
Simple library which allows you to build RESTful API fast, easy and flexible
Installation
Install it via composer
composer require pixaye/chiven
Usage
For start using Chiven`s functionality, you should initialize its Request class as early, as you can (for example, you can init it in index.php)
Initializating from globals
$request = new \Chiven\Http\Request(); $request->fromGlobals();
Initializating from custom variables
$request = new \Chiven\Http\Request(); $files = array( 'file' => array ( 'tmp_name' => '/tmp/df23fr32, 'name' => 'file.jpg', 'type' => 'image/jpeg', 'size' => 335057, 'error' => 0, ) ); $headers = array( 'X-Test-Header: 1', 'X-Test-Header: 2', ); $get = array( 'key' => 'value' ); $post = array( 'key' => 'value' ); $request->initialize($files, $get, $post, $headers)
Chiven allows you to work with files and headers in object oriented style. It builds objects of them and puts it in repositories: FileRepostiory and HeaderRepository
You can get them by calling $request->getFiles() and $request->getHeaders()
$filesRepository = $request->getFiles(); $testFile = $filesRepository->findBy('name', 'test');
$headersRepository = $request->getHeaders(); $testHeader = $filesRepository->findBy('name', 'X-Test-Header');
Both repositories have methods:
- findBy($criteria, $value)
- findLast()
- findFirst()
- findAll()
- insert(Insertable $object)
- remove($criteria, $value)
- set(array $objects)
To make Chiven process the request correctly and display the result in the desired format, there are format classes. At the moment, only JSON format is available to use.
$request = new \Chiven\Http\Request(); $request->fromGlobals(); (new \Chiven\Bootstrap())->setFormat(new \Chiven\Format\Json()); //Request handling... //Chiven response which returned by any script/controller/etc... $response = new \Chiven\Http\Response\Response(); echo $chiven->getFormat()->responseDecorator($response);
This is how Chiven works, you can use example above and start creating your API with Chiven.