pahanini / yii2-rest-doc

Yii2 REST doc generator
36 stars 19 forks source link

Getting unknown property TestController::modelClass #21

Closed isudakoff closed 6 years ago

isudakoff commented 7 years ago

Why you search modelClass? I can use many models in one controller...

api/TestController.php

/**
 * Test Controller
 *
 * @restdoc-label Test controller
 *
 */
class TestController extends Controller
{
    public function behaviors()
    {
        $behaviors = parent::behaviors();
        $behaviors['authenticator'] = [
            'class' => \yii\filters\auth\CompositeAuth::className(),
            'authMethods' => [
                \yii\filters\auth\HttpBasicAuth::className(),
                \yii\filters\auth\HttpBearerAuth::className(),
                \yii\filters\auth\QueryParamAuth::className(),
            ],
        ];
        return $behaviors;
    }

    /**
     * Gets users profile info
     *
     * @return Profile
     *
     * @throws NotFoundHttpException
     */
    public function actionProfile()
    {
        return $this->findModel(Yii::$app->user->identity->id)->profile;
    }

    /**
     * Creates new entry of [[Network]] for current user
     *
     * << REST API USAGE >>
     *
     * @restdoc-field int $network Type of network from special list of networks
     * @restdoc-field string $access_token Access token received from social network
     * @restdoc-field string $secret_token Secret token received from social network
     * @restdoc-field string $refresh_token Refresh token received from social network
     * @restdoc-field string $token_type Token type received from social network
     * @restdoc-field string $network_user_id Id of user in current network
     * 
     * @return string If success
     */
    public function actionAddNetwork()
    {
        Network::deleteAll([
            'user_id' => Yii::$app->user->identity->id,
            'network' => Yii::$app->request->post('network'),
        ]);

        $social = new Network();
        $social->user_id = Yii::$app->user->identity->id;

        $social->load(Yii::$app->request->getBodyParams(), '');

        if ($social->save()) {
            return ['success' => true];
        }

        return $social;
    }

    public function findModel($id)
    {
        if (($model = User::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException("Object not found");
        }
    }
}
pahanini commented 7 years ago

What is a namespace of Controller in your example? Is it based on yii\rest\ActiveController?

isudakoff commented 7 years ago

@pahanini No. It is based on yii\rest\Controller

pahanini commented 7 years ago

This is because you use yii\rest\Controller instead of yii\rest\ActiveController. This library tries to determine which model to use using mandatory modelClass property of yii\rest\ActiveController.

So there are two options for you: