Ничего сложного Нет! Создаёте модуль api и подключаете его:
'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. Создаёте Контроллер наследуемый от 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. Настраиваете роуты например так:
[
'class' => 'yii\rest\UrlRule',
'controller' => ['api/v1/organizations'],
'extraPatterns' => [
'GET, POST find' => 'find',
],
],
4. В конфигах api примерно такое
'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,
],
],
],