Skip to content

Instantly share code, notes, and snippets.

@littlefuntik
Created January 4, 2016 17:04
Show Gist options
  • Save littlefuntik/eef1ccb32c4c5cc820fb to your computer and use it in GitHub Desktop.
Save littlefuntik/eef1ccb32c4c5cc820fb to your computer and use it in GitHub Desktop.
Yii2 rest Serializer example
<?php
namespace app\rest;
use yii\filters\AccessControl;
use yii\filters\auth\CompositeAuth;
use app\components\HeaderParamAuth;
/**
* Description of ActiveController
*
* @author user
*/
class ActiveController extends \yii\rest\ActiveController {
/**
* @inheritdoc
*/
public $serializer = [
'class' => 'app\rest\Serializer',
];
/**
* @inheritdoc
*/
protected function verbs()
{
$verbs = parent::verbs();
$verbs['update'] = ['POST'];
return $verbs;
}
/**
* @inheritdoc
*/
public function behaviors()
{
$behaviors = parent::behaviors();
$behaviors['authenticator'] = [
'class' => CompositeAuth::className(),
'authMethods' => [
HeaderParamAuth::className(),
],
];
$behaviors['access'] = [
'class' => AccessControl::className(),
'rules' => [
[
'allow' => true,
'roles' => ['@'],
],
],
];
return $behaviors;
}
}
<?php
namespace app\rest;
use yii\base\Model;
use yii\base\Arrayable;
class Serializer extends \yii\rest\Serializer
{
public $collectionEnvelope = 'items';
public function serialize($data) {
$_data = parent::serialize($data);
$dataResult = [
'code' => $this->response->getStatusCode(),
'status' => ($this->response->getIsSuccessful()) ? 'success' : 'error',
];
if (!$this->response->getIsOk()) {
$dataResult['message'] = $this->response->statusText;
}
if ($data instanceof Model && $data->hasErrors()) {
$dataResult['result'] = $_data;
// return $this->serializeModelErrors($data);
} elseif ($data instanceof Arrayable) {
$dataResult['result'] = $_data;
// return $this->serializeModel($data);
} elseif ($data instanceof DataProviderInterface) {
$dataResult['result'] = $_data;
// return $this->serializeDataProvider($data);
} else {
if (is_array($_data) && isset($_data[$this->collectionEnvelope])) {
$dataResult['result'] = $_data[$this->collectionEnvelope];
} else {
$dataResult['result'] = $_data;
}
}
if (!isset($dataResult['result']) || empty($dataResult['result'])) {
$dataResult['result'] = [];
}
return $dataResult;
}
}
<?php
namespace app\controllers;
use app\rest\ActiveController;
use app\models\TournamentFish;
class TournamentFishController extends ActiveController
{
public $modelClass = TournamentFish::class;
public function actions()
{
return array_merge(
parent::actions(),
[
'index' => [
'class' => 'app\rest\TournamentFish\IndexAction',
'modelClass' => $this->modelClass,
'checkAccess' => [$this, 'checkAccess'],
],
'view' => [
'class' => 'app\rest\TournamentFish\ViewAction',
'modelClass' => $this->modelClass,
'checkAccess' => [$this, 'checkAccess'],
],
'create' => [
'class' => 'app\rest\TournamentFish\CreateAction',
'modelClass' => $this->modelClass,
'checkAccess' => [$this, 'checkAccess'],
],
'update' => [
'class' => 'app\rest\TournamentFish\UpdateAction',
'modelClass' => $this->modelClass,
'checkAccess' => [$this, 'checkAccess'],
],
'delete' => [
'class' => 'app\rest\TournamentFish\DeleteAction',
'modelClass' => $this->modelClass,
'checkAccess' => [$this, 'checkAccess'],
],
]
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment