A
A
AlexAll2019-07-14 07:20:45
Yii
AlexAll, 2019-07-14 07:20:45

How to make api yii2 basic?

Hello, I followed this instruction https://www.diggin-data.de/dd-cms/blog/post/view/i... but when I try to follow the link site.local/api/v1/project there is silence
What is there not this way?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim, 2019-07-20
@AlexAll

Nothing complicated No! Create an api module and include it:

'modules' => [
        'api' => [
            'class' => app\modules\api\Module::class,
            'modules' => [
                'v1' => [
                    'class' => app\modules\api\modules\v1\Module::class,
                    'controllerMap' => [
                        'organizations' => \app\modules\organizations\api\controllers\DefaultController::class,
                        'users' => \app\modules\users\api\Controllers\DefaultController::class,
                        'cities' => \app\modules\api\modules\v1\controllers\events\CitiesController::class,
                    ],
                ]
            ],
        ],
    ],

2. Create a Controller that inherits from ActiveController
class DefaultController extends ActiveController
{
    public $modelClass = Organization::class;
    public $serializer = [
        'class' => 'yii\rest\Serializer',
        'collectionEnvelope' => 'items',
    ];

    public function behaviors()
    {
        $behaviors = parent::behaviors();
        $behaviors['contentNegotiator']['formats']['text/html'] = Response::FORMAT_JSON;
        return $behaviors;
    }

    public function actions()
    {
        $actions = parent::actions();
        unset($actions['delete'], $actions['create'], $actions['view']);
        return $actions;
    }
}

3. Set up routes like this:
[
                    'class' => 'yii\rest\UrlRule',
                    'controller' => ['api/v1/organizations'],
                    'extraPatterns' => [
                        'GET, POST find' => 'find',
                    ],
                ],

4. In api configs something like this
'components' => [
        'response' => [
            // ...
            'formatters' => [
                \yii\web\Response::FORMAT_JSON => [
                    'class' => yii\web\JsonResponseFormatter::class,
                    'prettyPrint' => YII_DEBUG, // используем "pretty" в режиме отладки
                    'encodeOptions' => JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE,
                ],
            ],
        ],
        'as contentNegotiator' => [
            'class' => \yii\filters\ContentNegotiator::class,
            'formatParam' => '_format',
            'formats' => [
                'application/json' => \yii\web\Response::FORMAT_JSON,
                'application/octet-stream' => \yii\web\Response::FORMAT_JSON,
                'text/html' => \yii\web\Response::FORMAT_JSON,
                'application/xml' => \yii\web\Response::FORMAT_XML,
            ],
        ],
    ],

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question