Swagger-php on Yii2 ActiveController


(Robinelst) #1

I also posted my question on STackOverflow (44032555) but without success so trying it on this forum:

I have a REST api running in my yii2 app. I extend my api model from my AR model and use the fields() function to define the api response fields.


/**

 * @SWG\Definition(

 *   type="object"

 * )

 */    

class Person extends \common\models\Person implements Linkable {


    public function fields() {

        return ['id',

                'name',

                'first-name' => 'first_name'

        ];

    }    


    public function getLinks() {

        return [

                Link::REL_SELF  => Url::to(['person/view', 'id' => $this->id], true),

        ];

    }



My activecontroller is the easiest implementation

use yii\rest\ActiveController;




/**

 * @SWG\Info(title="My First API", version="0.1")

 */

class PersonController extends ActiveController {


    public $modelClass = 'api\modules\v1\models\Person';


}



So far, everything works. Then I wanted to add Swagger-php to document my API: I use light/yii2-swagger. The configuration works and I have my swagger-ui page/json.

The next step is to start adding documentation… But in all the Swagger-php examples or on github.com/lichunqiang/yii2-swagger-demo, the annotations are on the modelfields and the controller methods. The problem is that those are not explicitly in the code because of ActiveRecord and the fields() function.

Is there are way to combine swagger-php and ActiveController?