1. Go to this page and download the library: Download xp-forge/rest-api 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/ */
xp-forge / rest-api example snippets
use web\rest\{Get, Post, Resource, Response};
#[Resource('/users')]
class Users {
#[Get('/')]
public function listUsers($max= 10) {
// $max comes from request parameter "max", defaulting to 10
// ...
}
#[Get('/{id}')]
public function findUser($id) {
// $id is extracted from URL segment
// ...
}
#[Post('/')]
public function createUser($user) {
// $user is deserialized from the request body according to its content type
// ...
return Response::created('/users/{id}', $id)->entity($created);
}
}
use web\Application;
class Service extends Application {
/** @return [:var] */
public function routes() {
return ['/users' => new RestApi(new Users())];
}
}
use web\rest\{Resource, Get, Param, SeparatedBy};
#[Resource('/api/trainings')]
class Trainings {
#[Get('/completed')]
public function completed(#[Param, SeparatedBy(',')] array $orgunits) {
return $this->repository->find(['status' => 'COMPLETED', 'orgunits' => $orgunits]);
}
}
use web\rest\{Resource, Get, Matrix};
#[Resource('/api/trainings')]
class Trainings {
#[Get('/{filter}/authors')]
public function authors(#[Matrix] array $filter) {
$authors= [];
foreach ($this->repository->find($filter) as $training) {
$authors[$training->author->id()]= $training->author;
}
return $authors;
}
}
use io\Folder;
use web\rest\{Async, Post, Resource, Response};
#[Resource('/api')]
class Uploads {
public function __construct(private Folder $folder) { }
#[Post('/files')]
public function upload(#[Request] $req) {
return new Async(function() use($req) {
if ($multipart= $req->multipart()) {
foreach ($multipart->files() as $file) {
yield from $file->transmit($this->folder);
}
}
return Response::ok();
});
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.