understanding response headers

Hey guys.

I’m trying to understand why:


public function actionFakeApi2($id)

{

	Yii::$app->response->headers->set('Content-Type', 'image/png');

	return file_get_contents(Yii::getAlias("@webroot/images/{$id}.png"));

}

Does not display an image (Content-Type: text/html)

But


public function actionFakeApi2($id)

{

	Yii::$app->response->headers->set('Content-Type', 'image/png');

	Yii::$app->response->send();

	echo file_get_contents(Yii::getAlias("@webroot/images/{$id}.png"));

}

Does?

Shouldn’t both behave the same? Is there a reason why the logic is separated and the headers aren’t applied on a return?

It is worth noting that in the second case if I replace "echo" with "return" it does not work either.

You should set Yii::$app->response->format = Response::FORMAT_RAW; if you want to "return $fileContent".

The value you return from an action is set as Response::data which will be formatted before sending to browser.

Thanks!! I never checked the API on this one, only the guide documentation that does not mention the RAW format. I will make a pull request with the addition to the documentation