1. Go to this page and download the library: Download pocifik/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/ */
pocifik / yii2-swagger example snippets
namespace app\controllers;
use Yii;
use yii\helpers\Url;
use yii\web\Controller;
/**
* @SWG\Swagger(
* basePath="/",
* produces={"application/json"},
* consumes={"application/x-www-form-urlencoded"},
* @SWG\Info(version="1.0", title="Simple API"),
* )
*/
class SiteController extends Controller
{
/**
* @inheritdoc
*/
public function actions(): array
{
return [
'docs' => [
'class' => 'yii2mod\swagger\SwaggerUIRenderer',
'restUrl' => Url::to(['site/json-schema']),
],
'json-schema' => [
'class' => 'yii2mod\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
{
/**
* @SWG\Get(path="/user",
* tags={"User"},
* summary="Retrieves the collection of User resources.",
* @SWG\Response(
* response = 200,
* description = "User collection response",
* @SWG\Schema(ref = "#/definitions/User")
* ),
* )
*/
public function actionIndex()
{
$dataProvider = new ActiveDataProvider([
'query' => User::find(),
]);
return $dataProvider;
}
}
namespace app\models\definitions;
/**
* @SWG\Definition(rty(property="email", type="string")
* @SWG\Property(property="username", type="string")
*/
class User
{
}