1. Go to this page and download the library: Download baraja-core/structured-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/ */
baraja-core / structured-api example snippets
declare(strict_types=1);
namespace App\Model;
final class MyAwesomeEndpoint extends BaseEndpoint
{
/**
* This is test API endpoint as demonstration of inner logic.
*
* @param string $hello some user-defined parameter.
*/
public function actionDefault(string $hello = 'world'): MyAwesomeResponse
{
// The endpoint response can be simply returned as an simple array or typed object.
// A type object is much better because it will be used as the basis for documentation.
// It will be automatically converted to JSON.
return new MyAwesomeResponse(
name: 'Test API endpoint',
hello: $hello,
);
}
// or use old syntax:
/**
* This is test API endpoint as demonstration of inner logic.
*
* @param string $hello some user-defined parameter.
*/
public function actionDefault(string $hello = 'world'): void
{
$this->sendJson([
'name' => 'Test API endpoint',
'hello' => $hello,
]);
}
// or return simple array directly:
/**
* @param array<mixed, mixed> $data
*/
public function postCreateUser(array $data): array
{
return [
'state' => 'ok',
'data' => $data,
];
}
}
final class MyAwesomeEndpoint extends BaseEndpoint
{
public function getDefault(): void
{
// this logic will be called as GET.
}
public function actionDefault(): void
{
// this logic will be called as GET too,
// because `action` prefix is alias for GET.
}
public function postDetail(string $id, string $name, ?string $description = null): void
{
// this logic will be called as POST.
}
}
final class OrderEndpoint extends BaseEndpoint
{
public function postProcessOrder(array $data): void
{
// variable $data contains all raw data from user.
}
}
final class ArticleEndpoint extends BaseEndpoint
{
/**
* @param string|null $locale in format "cs" or "en"
* @param int $page real page number for filtering, 1 => first page ... "n" page
* @param int $limit in interval <0, 500)
* @param string|null $status matching constant self::STATUS_* (null, all, published, trash)
* @param string|null $query smart search query
* @param string|null $filterFrom find all articles from this date
* @param string|null $filterTo find all articles to this date
* @param string|null $sort sort by supported field
* @param string|null $orderBy direction by `ASC` or `DESC`
*/
public function actionDefault(
?string $locale = null,
int $page = 1,
int $limit = 32,
?string $status = null,
?string $query = null,
?string $filterFrom = null,
?string $filterTo = null,
?string $sort = null,
?string $orderBy = null,
): void {
}
}
final class ArticleEndpoint extends BaseEndpoint
{
public function actionDetail(string $id): void
{
// your logic for fetch data from database
// your response
$this->sendJson([
'id' => $id,
'title' => 'My awesome article',
'content' => '...',
]);
}
}
final class ArticleEndpoint extends BaseEndpoint
{
public function createDetail(string $title, string $content, ?string $perex = null): void
{
try {
// creating in database...
$this->sendOk([
'id' => 123,
]);
} catch (\Exception $e) {
$this->sendError('Can not create article because ...');
}
}
}
#[PublicEndpoint]
final class ProductEndpoint extends BaseEndpoint
{
}
#[Role(roles: ['admin', 'moderator'])]
final class ArticleEndpoint extends BaseEndpoint
{
}
#[PublicEndpoint]
final class SitemapEndpoint extends BaseEndpoint
{
#[Role(roles: 'admin')]
public function actionClearCache(): void
{
// your secured implementation
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.