PHP code example of abdphyr / swagger

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

    

abdphyr / swagger example snippets


// pages
return [
    'default' => [
        ...,
        'document' => [...] // This properties matches fully `OPEN API`s configuration.
    ],
    'admin' => [...],
    'otherpage' => [...]
];

namespace App\Http\Requests;

use Abdphyr\Swagger\Attributes\DTO\FileParam;
use Abdphyr\Swagger\Attributes\DTO\RequestParam;
use Illuminate\Http\UploadedFile;

#[RequestParam(key: 'name', value: 'Rich dad and poor dad')]
#[RequestParam(key: 'year', value: '1997')]
#[RequestParam(key: 'series', value: 'DWAR')]
#[FileParam(key: 'file', value: new UploadedFile('path/to/file'))]
class BookUpsertRequest extends BaseRequest
{
    public function rules(): array
    {
        return [
            'name' => ['

namespace App\Http\Controllers;

use Abdphyr\Swagger\Attributes\Http\ActionMethod;
use Abdphyr\Swagger\Attributes\Http\ActionController;
use App\Services\BookService;

// Absolute path: http://localhost:8000/api/book
// This controller API data visible `default` page
#[ActionController(uri: 'book', pages: ['defaut'])]
class BookController extends Controller
{
    public function __construct(protected BookService $service) {}

    // Path: http://localhost:8000/api/book?name=Rich...
    // "auth: false" means this endpoint don't h: http://localhost:8000/api/book
    #[ActionMethod(uri: '', method: 'POST', summary: 'Creating Book')]
    public function store(BookUpsertRequest $request)
    {
        return $this->service->create($request->validated());
    }

    // Path: http://localhost:8000/api/book-top-all
    // Absolute path is not used beacuse of `uri` starting with `/` that marks this `uri` is `ABSOLUTE PATH`
    #[ActionMethod(uri: '/book-top-all', method: 'GET', , summary: 'Top books')]
    public function topBooks()
    {
        return $this->service->topBooks();
    }
}

// config/swagger.php
return [
    // page name
    'default' => [...]
];

// PAGE NAME: `default`
// COMMAND: php artisan generate:swagger `default`
// UI: http://localhost:8000/swagger/default-ui
// DATA: http://localhost:8000/swagger/default-data
sh
php artisan vendor:publish --provider="Abdphyr\Swagger\SwaggerServiceProvider"
sh
php artisan generate:swagger default
sh
php artisan generate:swagger default --c=ExampleController
php artisan generate:swagger default --controller=ExampleController
sh
php artisan generate:swagger default --c=ExampleController --method=show
php artisan generate:swagger default --controller=ExampleController --m=show
sh
php artisan generate:swagger default --rm --c=ExampleController
php artisan generate:swagger default --rm --controller=ExampleController --m=show
sh
php artisan generate:swagger default --clear