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);
}
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);
}