Trait luya\traits\RestBehaviorsTrait
Rest Behaviors Trait.
This class overrides the default behaviors method of {{yii\rest\Controller}} controllers.
The following changes are differ to the base implementation:
- If {{luya\rest\UserBehaviorInterface}} is not implemented, the
authenticator
behavior ({{yii\filters\auth\CompositeAuth}}) is removed. - If {{luya\rest\UserBehaviorInterface}} is implemented, the
authenticator
behavior ({{yii\filters\auth\CompositeAuth}}) is enabled. - If {{luya\rest\UserBehaviorInterface}} is implemented, the
contentNegotiator
behavior ({{yii\filters\ContentNegotiator}}) is enabled. - The
rateLimiter
behavior filter is removed by default.
Read the {{luya\rest\UserBehaviorInterface}} about the configuration ability to protect the controller.
Public Properties
Property | Type | Description | Defined By |
---|---|---|---|
$authOptional | array | List of action IDs that this filter will be applied to, but auth failure will not lead to error. | luya\traits\RestBehaviorsTrait |
$enableCors | boolean | Whether CORS should be enabled or not. | luya\traits\RestBehaviorsTrait |
$jsonCruft | boolean | Whether a unparsable cruf should be added to the json response or not. | luya\traits\RestBehaviorsTrait |
$languages | array | An array with languages which are passed to {{yii\filters\ContentNegotiator::$languages}}. | luya\traits\RestBehaviorsTrait |
Public Methods
Method | Description | Defined By |
---|---|---|
behaviors() | Override the default {{yii\rest\Controller::behaviors()}} method. | luya\traits\RestBehaviorsTrait |
getCompositeAuthMethods() | Return all Auth methods for Composite Auth. | luya\traits\RestBehaviorsTrait |
sendArrayError() | Send Array validation error. | luya\traits\RestBehaviorsTrait |
sendModelError() | Send Model errors with correct headers. | luya\traits\RestBehaviorsTrait |
Property Details
List of action IDs that this filter will be applied to, but auth failure will not lead to error.
It may be used for actions, that are allowed for public, but return some additional data for authenticated users.
Defaults to empty, meaning authentication is not optional for any action.
Since version 2.0.10 action IDs can be specified as wildcards, e.g. site/*
.
Whether CORS should be enabled or not.
Whether a unparsable cruf should be added to the json response or not. When enabled you have to parse the json response first before interpreting as json.
An array with languages which are passed to {{yii\filters\ContentNegotiator::$languages}}. Example
'languages' => [
'en',
'de',
],
Method Details
Override the default {{yii\rest\Controller::behaviors()}} method.
The following changes are differ to the base implementation:
- If {{luya\rest\UserBehaviorInterface}} is not implemented, the
authenticator
behavior ({{yii\filters\auth\CompositeAuth}}) is removed. - If {{luya\rest\UserBehaviorInterface}} is implemented, the
authenticator
behavior ({{yii\filters\auth\CompositeAuth}}) is enabled. - If {{luya\rest\UserBehaviorInterface}} is implemented, the
contentNegotiator
behavior ({{yii\filters\ContentNegotiator}}) is enabled. - The
rateLimiter
behavior filter is removed by default.
public array behaviors ( ) | ||
return | array |
Returns an array with registered behavior filters based on the implementation type. |
---|
public function behaviors()
{
$behaviors = parent::behaviors();
if ($this->enableCors) {
$behaviors['cors'] = Yii::$app->corsConfig;
}
unset($behaviors['authenticator']);
if ($this->getUserAuthClass()) {
// change to admin user auth class
$behaviors['authenticator'] = [
'class' => CompositeAuth::class,
'user' => $this->getUserAuthClass(),
'authMethods' => $this->getCompositeAuthMethods(),
'optional' => $this->authOptional,
];
if ($this->enableCors) {
$behaviors['authenticator']['except'] = ['options'];
}
}
$behaviors['contentNegotiator'] = [
'class' => ContentNegotiator::class,
'formats' => [
'application/json' => Response::FORMAT_JSON,
'application/xml' => Response::FORMAT_XML,
],
'languages' => $this->languages,
];
// by default rate limiter behavior is removed as it requires a database
// user given from the admin module.
if (isset($behaviors['rateLimiter'])) {
unset($behaviors['rateLimiter']);
}
if ($this->jsonCruft) {
$behaviors['cruft'] = JsonCruftFilter::class;
}
return $behaviors;
}
Return all Auth methods for Composite Auth.
public array getCompositeAuthMethods ( ) |
public function getCompositeAuthMethods()
{
return [
QueryParamAuth::class,
HttpBearerAuth::class,
];
}
Send Array validation error.
Example input:
return $this->sendArrayError(['firstname' => 'Firstname cannot be blank']);
Example return value:
Array
(
[0] => Array
(
[field] => firstname
[message] => Firstname cannot be blank.
)
)
public array sendArrayError ( array $errors ) | ||
$errors | array |
Provide an array with messages. Where key is the field and value the message. |
return | array |
Returns an array with field and message keys for each item. |
---|
public function sendArrayError(array $errors)
{
return RestHelper::sendArrayError($errors);
}
Send Model errors with correct headers.
Helper method to correctly send model errors with the correct response headers.
Example return value:
Array
(
[0] => Array
(
[field] => firstname
[message] => Firstname cannot be blank.
)
[1] => Array
(
[field] => email
[message] => Email cannot be blank.
)
)
public array sendModelError ( yii\base\Model $model ) | ||
$model | yii\base\Model |
The model to find the first error. |
return | array |
If the model has errors InvalidParamException will be thrown, otherwise an array with message and field key. |
---|---|---|
throws | yii\base\InvalidParamException |
public function sendModelError(Model $model)
{
return RestHelper::sendModelError($model);
}