PHP code example of didasto / apilot

1. Go to this page and download the library: Download didasto/apilot 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/ */

    

didasto / apilot example snippets


'providers' => [
    Didasto\Apilot\ApilotServiceProvider::class,
],



namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    protected $fillable = ['title', 'body', 'status'];
}



namespace App\Http\Controllers\Api;

use Didasto\Apilot\Controllers\ModelCrudController;
use Didasto\Apilot\Enums\AllowedFilter;
use App\Models\Post;

class PostController extends ModelCrudController
{
    protected string $model = Post::class;
    protected array $allowedFilters = ['status' => AllowedFilter::EXACT];
    protected array $allowedSorts   = ['title', 'created_at'];
}

use Didasto\Apilot\Routing\CrudRouteRegistrar;
use App\Http\Controllers\Api\PostController;

CrudRouteRegistrar::resource('posts', PostController::class);



namespace App\Services;

use Didasto\Apilot\Contracts\CrudServiceInterface;
use Didasto\Apilot\Dto\PaginatedResult;
use Didasto\Apilot\Dto\PaginationParams;

class ProductService implements CrudServiceInterface
{
    public function list(array $filters, array $sorting, PaginationParams $pagination): PaginatedResult
    {
        // Fetch from external API or custom source
        return new PaginatedResult(items: [], total: 0, perPage: $pagination->perPage, currentPage: $pagination->page);
    }

    public function find(int|string $id): mixed { /* ... */ }
    public function create(array $data): mixed   { /* ... */ }
    public function update(int|string $id, array $data): mixed { /* ... */ }
    public function delete(int|string $id): bool { /* ... */ }
}



namespace App\Http\Controllers\Api;

use App\Services\ProductService;
use Didasto\Apilot\Controllers\ServiceCrudController;

class ProductController extends ServiceCrudController
{
    protected string $serviceClass = ProductService::class;
}

CrudRouteRegistrar::resource('products', ProductController::class)->only(['index', 'show']);

use Didasto\Apilot\Enums\AllowedFilter;

protected array $allowedFilters = [
    'status' => AllowedFilter::EXACT,   // WHERE status = ?
    'title'  => AllowedFilter::PARTIAL, // WHERE title LIKE %?%
    'status' => AllowedFilter::SCOPE,   // $query->status($value) — calls a model scope
];

protected array $allowedSorts = ['title', 'created_at', 'status'];



namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use Didasto\Apilot\Controllers\ModelCrudController;
use App\Models\Post;

class PostController extends ModelCrudController
{
    protected string $model = Post::class;

    protected function beforeStore(array $data, Request $request): array
    {
        // Automatically attach the authenticated user
        $data['user_id'] = $request->user()->id;
        return $data;
    }

    protected function modifyIndexQuery(mixed $query, Request $request): mixed
    {
        // Show only the authenticated user's posts
        return $query->where('user_id', $request->user()->id);
    }

    protected function beforeDestroy(mixed $item, Request $request): bool
    {
        // Only the owner may delete their post
        return $item->user_id === $request->user()->id;
    }
}

use Didasto\Apilot\Attributes\OpenApiMeta;

#[OpenApiMeta(summary: 'Blog Posts', description: 'Manage blog posts.', tags: ['Posts'])]
class PostController extends ModelCrudController { ... }

use Didasto\Apilot\Attributes\OpenApiProperty;

class PostRequest extends FormRequest
{
    #[OpenApiProperty(properties: [
        'published_at' => ['type' => 'string', 'format' => 'date-time', 'nullable' => true],
    ])]
    public function rules(): array { ... }
}

// In routes/api.php
CrudRouteRegistrar::resource('posts', PostController::class)
    ->middleware(['apilot.json', 'auth:sanctum']);

// Or globally in app/Http/Kernel.php (Laravel 10 and earlier)
protected $middlewareGroups = [
    'api' => [
        \Didasto\Apilot\Http\Middleware\ForceJsonResponse::class,
        // ...
    ],
];

// Laravel 11+ bootstrap/app.php
->withMiddleware(function (Middleware $middleware) {
    $middleware->appendToGroup('api', \Didasto\Apilot\Http\Middleware\ForceJsonResponse::class);
})

interface CrudServiceInterface
{
    // Return a paginated list of items, applying filters and sorting.
    public function list(array $filters, array $sorting, PaginationParams $pagination): PaginatedResult;

    // Return a single item by ID, or null if not found.
    public function find(int|string $id): mixed;

    // Create and return a new item.
    public function create(array $data): mixed;

    // Update and return the item with the given ID.
    public function update(int|string $id, array $data): mixed;

    // Delete the item. Returns true on success.
    public function delete(int|string $id): bool;
}
bash
php artisan vendor:publish --tag=apilot

GET /api/posts?page=2&per_page=25