PHP code example of breadhead / yii2-swagger

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

    

breadhead / yii2-swagger example snippets




namespace app\controllers;

use Yii;
use yii\helpers\Url;
use yii\web\Controller;

/**
 * 
 * @SWG\Info(version="1.0", title="Simple API")
 */
class SiteController extends Controller
{
    /**
     * @inheritdoc
     */
    public function actions(): array
    {
        return [
            'docs' => [
                'class' => 'breadhead\swagger\SwaggerUIRenderer',
                'restUrl' => Url::to(['site/json-schema']),
            ],
            'json-schema' => [
                'class' => 'breadhead\swagger\OpenAPIRenderer',
                // Тhe list of directories that contains the swagger annotations.
                'scanDir' => [
                    Yii::getAlias('@app/controllers'),
                    Yii::getAlias('@app/models'),
                ],
            ],
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
        ];
    }
}

namespace app\controllers;

use app\models\User;
use yii\data\ActiveDataProvider;
use yii\rest\Controller;

/**
 * Class UserController
 */
class UserController extends Controller
{
    /**
     * @OA\Get(path="/user",
     *     tags={"User"},
     *     summary="Retrieves the collection of User resources.",
     *     @OA\Response(
     *         response = 200,
     *         description = "User collection response",
     *         @OA\Schema(ref = "#/components/schemas/User")
     *     ),
     * )
     */
    public function actionIndex()
    {
        $dataProvider = new ActiveDataProvider([
            'query' => User::find(),
        ]);

        return $dataProvider;
    }
}



namespace app\models\definitions;

/**
 * @OA\SCHEMA(operty(property="email", type="string")
 * @OA\Property(property="username", type="string")
 */
class User
{
}

'urlManager' => [
    'enablePrettyUrl' => true,
    'enableStrictParsing' => true,
    'showScriptName' => false,
    'rules' => [
        'GET,HEAD users' => 'user/index',
    ],
]

'request' => [
    'parsers' => [
        'application/json' => 'yii\web\JsonParser',
    ]
]

php composer.phar