Library helps you build a documentation for your api, and you can even use it to validate request parameters
- Add composer dependency
"briedis/api-builder": "~1.0"
and runcomposer update
- Add service provider
Briedis\ApiBuilder\ApiBuilderLaravel4ServiceProvider
- Publish assets public directory:
php artisan asset:publish briedis/api-builder
- Add composer dependency
"briedis/api-builder": "~1.0"
and runcomposer update
- Add service provider
Briedis\ApiBuilder\ApiBuilderLaravel5ServiceProvider
- Publish assets to public directory:
php artisan vendor:publish --force --provider="Briedis\ApiBuilder\ApiBuilderLaravel5ServiceProvider"
(force means that existing files will be overwritten)
use Briedis\ApiBuilder\Method;
use Briedis\ApiBuilder\StructureBuilder;
class ExampleGetUserRequest extends Method{
const URI = 'user/get';
const METHOD = 'GET';
public $title = 'User information';
public $description = 'Get user by given ids. One or multiple users can be fetched at once';
public function getRequest(){
return new GetUsersStructure;
}
public function getResponse(){
return (new StructureBuilder)
->struct('users', new UserStructure, 'Array with user objects')->multiple();
}
}
use Briedis\ApiBuilder\StructureBuilder;
use Briedis\ApiBuilder\StructureInterface;
class GetUsersStructure implements StructureInterface {
/**
* Get the structure object
* @return \Briedis\ApiBuilder\StructureBuilder
*/
public function getStructure(){
return (new StructureBuilder)
->int('userId', 'Array of user ids you want to fetch')->multiple()
->int('offset', 'For paging purposes')->optional()
->int('count', 'Amount of users to fetch. Defaults to 20')->optional();
}
}
class UserStructure implements StructureInterface{
/**
* Get User structure object
* @return StructureBuilder
*/
public function getStructure(){
return (new StructureBuilder('User'))
->int('id', 'Unique identifier')
->str('username', 'Nickname that will be used in the system')
->str('firstName', 'Users first name')
->str('lastName', 'Users last name')
->str('gender', 'M - male, F - female')->values(['M', 'F'])->optional()
->int('signature', 'Provide your favorite quote or something, if you want')->optional()
->struct('location', new LocationStructure, 'Location object for the user')->optional()
->int('createdAt', 'Unix timestamp, when user has registered');
}
}
$presenter = new \Briedis\ApiBuilder\Presenter([
new ExampleGetUserRequest,
// Add request class instances as needed
]);
$presenter->setDomain('http://example/api/v1'));
echo $presenter->render();
- Automatic route generating