1. Go to this page and download the library: Download dskripchenko/laravel-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/ */
dskripchenko / laravel-api example snippets
// 1. Define your API class
class Api extends \Dskripchenko\LaravelApi\Components\BaseApi
{
public static function getMethods(): array
{
return [
'controllers' => [
'user' => [
'controller' => UserController::class,
'actions' => ['list', 'show', 'create'],
],
],
];
}
}
// 2. Define your module
class ApiModule extends \Dskripchenko\LaravelApi\Components\BaseModule
{
public function getApiVersionList(): array
{
return ['v1' => Api::class];
}
}
// 3. Define your ServiceProvider
class ApiServiceProvider extends \Dskripchenko\LaravelApi\Providers\ApiServiceProvider
{
protected function getApiModule() { return new ApiModule(); }
}
// 4. Write a controller with docblocks
class UserController extends \Dskripchenko\LaravelApi\Controllers\ApiController
{
/**
* List users
* @input integer ?$page Page number
* @output integer $id User ID
* @output string $name User name
*/
public function list(Request $request): JsonResponse
{
return $this->success(User::paginate()->toArray());
}
}
// V1: full API
class ApiV1 extends BaseApi {
public static function getMethods(): array {
return ['controllers' => [
'user' => [
'controller' => UserControllerV1::class,
'actions' => ['list', 'show', 'create', 'update', 'delete'],
],
]];
}
}
// V2: inherits V1, modifies selectively
class ApiV2 extends ApiV1 {
public static function getMethods(): array {
return ['controllers' => [
'user' => [
'controller' => UserControllerV2::class, // upgraded controller
'actions' => [
'delete' => false, // removed in v2
'archive', // new in v2
],
],
]];
}
}
class Api extends BaseApi {
public static $useResponseTemplates = true;
public static function getOpenApiTemplates(): array {
return [
'OrderResponse' => [
'id' => 'integer!', // => 'string(email)!', // format +
'OrderItem' => [
'product_id' => 'integer!',
'quantity' => 'integer',
'price' => 'number',
],
];
}
public static function getOpenApiSecurityDefinitions(): array {
return [
'BearerAuth' => ['type' => 'apiKey', 'name' => 'Authorization', 'in' => 'header'],
];
}
}
use Dskripchenko\LaravelApi\Services\OpenApi\DocLink;
DocLink::url('v1', 'order', 'create', 'post');
// https://example.test/api/doc#v1/tag/order/POST/v1/order/create
DocLink::anchor('v1', 'order', 'create', 'post'); // the hash alone, without '#'
class ProductService extends CrudService {
public function meta(): Meta {
return (new Meta())
->string('name', 'Name')
->number('price', 'Price')
->select('status', 'Status', ['active', 'draft'])
->crud();
}
public function query(): Builder { return Product::query(); }
public function resource(Model $model): BaseJsonResource { return new BaseJsonResource($model); }
public function collection(Collection $c): BaseJsonResourceCollection { return BaseJsonResource::collection($c); }
}
bash
php artisan api:generate-types # All versions → resources/js/shared/api/types.ts
php artisan api:generate-types --api-version=v1 # Specific version
php artisan api:generate-types --output=frontend/src/api/types.ts # Custom path
bash
php artisan api:lint # report
php artisan api:lint --strict # fail on warnings too, for CI
php artisan api:lint --unrouted # also: public methods no action points at