Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding url UrlRule, Controller classes. Support handling of empty res…
…ources
- Loading branch information
Showing
5 changed files
with
80 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
/** | ||
* @author Anton Tuyakhov <atuyakhov@gmail.com> | ||
*/ | ||
|
||
namespace tuyakhov\jsonapi; | ||
|
||
|
||
use yii\filters\ContentNegotiator; | ||
use yii\helpers\ArrayHelper; | ||
use yii\web\Response; | ||
|
||
class Controller extends \yii\rest\Controller | ||
{ | ||
public $serializer = 'tuyakhov\jsonapi\Serializer'; | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function behaviors() | ||
{ | ||
return ArrayHelper::merge(parent::behaviors(), [ | ||
'contentNegotiator' => [ | ||
'class' => ContentNegotiator::className(), | ||
'formats' => [ | ||
'application/vnd.api+json' => Response::FORMAT_JSON, | ||
], | ||
] | ||
]); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
/** | ||
* @author Anton Tuyakhov <atuyakhov@gmail.com> | ||
*/ | ||
|
||
namespace tuyakhov\jsonapi; | ||
|
||
|
||
/** | ||
* UrlRule is provided to simplify the creation of URL rules for JSON API support. | ||
* @package tuyakhov\jsonapi | ||
*/ | ||
class UrlRule extends \yii\rest\UrlRule | ||
{ | ||
/** | ||
* @inheritdoc | ||
*/ | ||
public function init() | ||
{ | ||
$this->tokens = array_merge($this->tokens, array_merge([ | ||
'{relationship}' => '<name:\w+>' | ||
])); | ||
$this->patterns = array_merge($this->patterns, [ | ||
'DELETE {id}/relationships/{relationship}' => 'delete-relationship', | ||
'POST,PATCH {id}/relationships/{relationship}' => 'update-relationship', | ||
'GET {id}/{relationship}' => 'view-related', | ||
'{id}/{relationship}' => 'options' | ||
]); | ||
parent::init(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters