Answer the question
In order to leave comments, you need to log in
Answer the question
In order to leave comments, you need to log in
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,
],
]
],
],
],
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;
}
}
[
'class' => 'yii\rest\UrlRule',
'controller' => ['api/v1/organizations'],
'extraPatterns' => [
'GET, POST find' => 'find',
],
],
'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 questionAsk a Question
731 491 924 answers to any question