Yii2: Add statusCode to response object for catchAll config

Created on 7 Mar 2017  路  5Comments  路  Source: yiisoft/yii2

In the case of catchAll config, I suggest making the ability to add the statusCode for the response object:
So, instead of:
'catchAll' => ['site/maintenance']

Is to add the statusCode like:
'catchAll' => ['site/maintenance', 503]
Or:
'catchAll' => [ 'route' => 'site/maintenance', 'statusCode' => 503 ]

Because it always returns 200 which doesn't make sense in most of the cases.

Most helpful comment

@Kolyunya, it can also handled this way:
$headers = Yii::$app->response->headers; $headers->add('Retry-After', '30');

All 5 comments

I think this should be encapsulated inside a controller action. If I have multiple catchAll stubs I don't want to edit status codes in addition to action path and parameters.

Later on you'd think of setting the Retry-After header. Should it also go to the catchAll parameter?

I agree with @Kolyunya. That is easily doable in the action itself.

Thanks, I was able to add this line of code to the action and it solves the issue:
Yii::$app->response->statusCode = 503;

@Kolyunya, it can also handled this way:
$headers = Yii::$app->response->headers; $headers->add('Retry-After', '30');

@devmustafa glad you were able to solve the issue! And thanks for the tip! :+1:

Was this page helpful?
0 / 5 - 0 ratings

Related issues

AstRonin picture AstRonin  路  3Comments

Kolyunya picture Kolyunya  路  3Comments

jpodpro picture jpodpro  路  3Comments

Locustv2 picture Locustv2  路  3Comments

schmunk42 picture schmunk42  路  3Comments