PHP code example of auto-swagger / php-swagger-generator

1. Go to this page and download the library: Download auto-swagger/php-swagger-generator 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/ */

    

auto-swagger / php-swagger-generator example snippets


#[ApiSwagger(summary: 'Store user', tag: 'User')]

#[ApiSwaggerRequest(request: UserCreateRequest::class, description: 'Store user')]
public function store(UserCreateRequest $request): UserPaginatedResource
{
    // some Logic
}

#[ApiSwaggerQuery([
name: "name", 
description: "Search by user name",

#[ApiSwaggerQuery(name: "id", 

#[ApiSwagger(summary: 'List users', tag: 'User')]
#[ApiSwaggerQuery([
name: "search", 
description: "Search by name or email",
: 200, resource: UserResource::class, isPagination: true)]
public function index(Request $request): UserPaginatedResource
{
    $users = $this->userRepository
        ->filter($request->all())
        ->paginate($request->input('perPage', 10));
    
    return new UserPaginatedResource($users);
}

#[ApiSwaggerResponse(status: 200, resource: [
    'id' => 'integer',
    'name' => 'string',
    'email' => 'string',
])]

#[ApiSwaggerResponse(status: 200, resource: ApiResource::class, description: 'User details')]

#[ApiSwaggerResponse(status: 200, resource: Model::class, description: 'User details')]

use AutoSwagger\Attributes\ApiSwaggerResource;
use Illuminate\Http\Resources\Json\JsonResource;

#[ApiSwaggerResource(name: 'User', properties: [
    'id' => 'integer',
    'name' => 'string',
])]
class ApiResource extends JsonResource
{
    public function toArray($request): array
    {
        return [
            'name' => $this->name,
            'id' => $this->id
        ];
    }
}


declare(strict_types=1);

namespace App\Http\Resources\User;

use AutoSwagger\Laravel\Resources\PaginatedResource;

class UserPaginatedResource extends PaginatedResource
{
    public function initCollection()
    {
        return $this->collection->map(function ($user) {
            return new UserResource($user);
        });
    }
}

#[ApiSwagger(summary: 'Get all users', tag: 'User')]
#[ApiSwaggerResponse(status: 200, resource: UserResource::class, isPagination: true)]
public function index(Request $request): UserPaginatedResource
{
    $users = $this->userRepository->paginate($request->input('perPage', 10));
    return new UserPaginatedResource($users);
}
bash
# Configuration files
php artisan vendor:publish --tag=auto-swagger-config

# Views (optional)
php artisan vendor:publish --tag=auto-swagger-views

# Assets (optional)
php artisan vendor:publish --tag=auto-swagger-assets
bash
php artisan swagger:generate
bash
php artisan swagger:generate --format=json
bash
php artisan swagger:generate --format=yaml