PHP code example of kayne / swagger

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

    

kayne / swagger example snippets




namespace App\Dtos;

use Kayne\Swagger\BaseDto;

class CreateUserDto extends BaseDto
{
    public string $name;   // Just type hints!
    public string $email;
    public ?int $age = null;
}



namespace App\Http\Controllers;

use App\Dtos\CreateUserDto;
use Kayne\Swagger\Attributes\Api;

class UserController extends Controller
{
    #[Api(
        method: 'POST',
        path: '/api/users',
        tags: ['Users']
    )]
    public function store(CreateUserDto $dto)
    {
        // Auto-detected: request body, validation, response
        return response()->json($dto->toArray());
    }
}

Route::middleware(['dto'])->group(function () {
    Route::post('/api/users', [UserController::class, 'store']);
});

use Kayne\Swagger\FormRequestDto;

class AssessmentSetIndexRequest extends FormRequestDto
{
    public function rules(): array
    {
        return [
            'page' => ['nullable', 'integer', 'min:1'],
            'per_page' => ['nullable', 'integer', 'min:1', 'max:100'],
        ];
    }
}

// GET request → Auto-convert to query parameters
#[Api(method: 'GET', path: '/api/assessment-sets', tags: ['Assessment'])]
public function index(AssessmentSetIndexRequest $request) { }

// routes/api.php
Route::group(['middleware' => ['token']], function () {
    Route::get('/menu-tree', [MenuController::class, 'getMenuTree']);
});

// Controller - No security parameter needed!
#[Api(method: 'GET', path: '/api/menu-tree', tags: ['Menu'])]
public function getMenuTree() { }

#[Api(
    method: 'POST',
    path: '/api/upload',
    tags: ['Upload'],
    contentType: 'multipart/form-data' // or 'application/x-www-form-urlencoded'
)]
public function upload(UploadDto $dto) { }

class DestroyRangeRequest extends FormRequestDto
{
    public function rules(): array
    {
        return [
            'ids' => ['

/**
 * @OA\Post(
 *     path="/api/users",
 *     tags={"Users"},
 *     @OA\RequestBody(
 *         operty="name", type="string", minLength=3),
 *             @OA\Property(property="email", type="string", format="email"),
 *             @OA\Property(property="age", type="integer", minimum=18)
 *         )
 *     ),
 *     @OA\Response(response="201", description="Created")
 * )
 */
public function store(Request $request) {
    $validated = $request->validate([
        'name' => '

// DTO - No annotations needed!
class CreateUserDto extends BaseDto {
    public string $name;
    public string $email;
    public ?int $age = null;
}

// Controller - Only 3 fields!
#[Api(method: 'POST', path: '/api/users', tags: ['Users'])]
public function store(CreateUserDto $dto) { }

#[PropSmart Parameter Detection

Kayne-Swagger automatically detects parameters like .NET:


#[Api(
    method: 'POST|GET|PUT|PATCH|DELETE',  // Required
    path: '/api/path',                    // Required
    tags: ['Tag1', 'Tag2'],               // Required

    // All below are OPTIONAL (auto-generated if not provided)
    summary: 'Short description',         // Optional
    description: 'Detailed description',  // Optional
    responseType: ResponseDto::class,     // Optional
    responseCode: 201,                    // Optional (default: 200)
    security: ['bearerAuth'],            // Optional (auto-detect from middleware)
    contentType: 'application/json'      // Optional (default: 'application/json')
)]

#[Property(
    description: "Field description",     // Optional
    example: "example value",             // Optional
    format: "email|password|date",        // Optional
    minimum: 0,                           // Optional
    maximum: 100,                         // Optional
    minLength: 3,                         // Optional
    maxLength: 255,                       // Optional
    pattern: "/regex/",                   // Optional
    enum: ["option1", "option2"]          // Optional
)]

class ClassController extends Controller
{
    public function assignDocuments(AssignDocumentsRequest $request)
    {
        // ...
    }
}

class ClassController extends Controller
{
    #[Api(
        method: 'POST',
        path: '/api/v1/classes/assign-documents',
        tags: ['Class'],
        summary: 'Assign documents',
        security: ['bearerAuth']
    )]
    public function assignDocuments(AssignDocumentsRequest $request)
    {
        // ...
    }
}

// config/swagger.php
'middleware_security_map' => [
    'token' => 'bearerAuth',      // Auto-detect from route group
    'auth' => 'bearerAuth',
    'auth:sanctum' => 'bearerAuth',
    'jwt' => 'bearerAuth',
],
bash
php artisan vendor:publish --tag=swagger-config
bash
php artisan swagger:gen LoginController
bash
php artisan swagger:gen "App\Http\Controllers\Api\LoginController"
bash
php artisan swagger:gen "app/Http/Controllers/UserController.php"