1. Go to this page and download the library: Download huge/rest library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
huge / rest example snippets
$loader = cessaire charger les annotations
\Huge\IoC\Container\SuperIoC::registerLoader(array($loader, 'loadClass'));
$ioc = new \Huge\Rest\WebAppIoC('1.1', array(
'maxBodySize' => 1024 // taille max en octet des body (par défaut ini_get('post_max_size')). Un flux Json en PUT / POST ne pourra pas faire + d'1Ko dans cet exemple
));
/**
* EXEMPLE
* Ressource "Person" qui a pour chemin "person". Notre ressource produit en retour une structure JSON en v1 par défaut.
* Chaque opération de la classe prend par défaut du "application/vnd.person.v1+json" / "application/json".
* Si on surcharge sur la fonction @Consumes / @Produces alors la configuration de la fonction primera.
*
* @Component
* @Resource
* @Path("person")
*
* @Consumes({"application/vnd.person.v1+json", "application/json"})
* @Produces({"application/vnd.person.v1+json"})
*/
class Person {
/**
* @Autowired("Huge\Rest\Http\HttpRequest")
* @var \Huge\Rest\Http\HttpRequest
*/
private $request;
/**
* @Autowired("Huge\IoC\Factory\ILogFactory")
* @var \Huge\IoC\Factory\ILogFactory
*/
private $loggerFactory;
public function __construct() {}
/**
* @Get
* @Consumes({"text/plain"})
* @Produces({"text/plain"})
*/
public function ping() {
return HttpResponse::ok();
}
/**
* @Get
* @Path(":mNumber")
*/
public function get($id = '') {
$person = new \stdClass();
$person->id = $id;
return HttpResponse::ok()->entity($person);
}
/**
* @Delete
* @Path(":mNumber")
*/
public function delete($id = '') {
$person = new \stdClass();
$person->id = $id;
return HttpResponse::ok()->entity($person);
}
/**
* @Put
* @Path(":mNumber")
*/
public function put($id = '') {
// @Consumes retenu est celui de la classe (du json)
$requestBody = (object)$this->request->getEntity();
$requestBody->id = $id;
return HttpResponse::ok()->entity($requestBody);
}
/**
* Accepte le content-type application/json
* @Post
*/
public function post() {
$person = new \stdClass();
$person->id = uniqid();
return HttpResponse::ok()->code(201)->entity($person);
}
/**
* @Get
* @Path("search/?:oNumber/?:oNumber")
*/
public function search($numberA = '', $numberB = '') {
$query = $this->request->getParam('query');
$list = array();
for ($i = 0; $i < 5; $i++) {
$person = new \stdClass();
$person->id = uniqid();
$person->query = $query;
$person->a = $numberA;
$person->b = $numberB;
$list[] = $person;
}
return HttpResponse::ok()->entity($list);
}
public function getRequest() {
return $this->request;
}
public function setRequest($request) {
$this->request = $request;
}
public function getLoggerFactory() {
return $this->loggerFactory;
}
public function setLoggerFactory(\Huge\IoC\Factory\ILogFactory $loggerFactory) {
$this->loggerFactory = $loggerFactory;
}
}
$ioc = new \Huge\Rest\WebAppIoC('1.0');
$ioc->addBodyReaders(array(
'application/vnd.github.v1+json' => 'Huge\Rest\Process\Readers\JsonReader'
));
$ioc = new \Huge\Rest\WebAppIoC('1.0');
$ioc->addBodyWriters(array(
'application/vnd.github.v1+json' => 'Huge\Rest\Process\Writers\JsonWriter'
));
$ioc = new \Huge\Rest\WebAppIoC('1.0');
$ioc->addDefinitions(array(
array(
'class' => 'MyWebApi\Security\Authorization',
'factory' => \Huge\IoC\Factory\SimpleFactory::getInstance()
),array(
'class' => 'MyWebApi\Security\AuthorizationBis',
'factory' => \Huge\IoC\Factory\SimpleFactory::getInstance()
),array(
'class' => 'MyWebApi\PowerByFilter',
'factory' => \Huge\IoC\Factory\SimpleFactory::getInstance()
)
));
$ioc->addRequestFiltersMapping(array(
'MyWebApi\Security\Authorization' => '.*', /* applique le filtre sur toutes les ressources */
'MyWebApi\Security\AuthorizationBis' /* on ne tient pas compte des paths */
));
$ioc->addResponseFiltersMapping(array(
'MyWebApi\PowerByFilter' => '.*'
));
// dans votre classe ressource
/**
* @Autowired("Huge\Rest\Http\BodyReader")
*/
private $bodyReader;
// dans votre fonction
$this->bodyReader->validateEntity('...nom_de_la_classe_modele...');
// ou
$this->bodyReader->validateEntityList('...nom_de_la_classe_modele...'); // si le contenu est une liste