call api from backend - header response content-type change


(Trinkity) #1

Hi,

My Yii2 application has advanced template.

App has 3 endpoints

rest

backend

frontend

And I wanted to call api methods from backend or frontend.

It work fine with this call in the backend controller




$response = Yii::$app->runAction('api/availability/create_availability');



But the problem is that when return create_availability, the content type of page change because i call internal api function and i can’t render the page in the backend controller.

How can i call the api from backend without change header content-type ?


(Trinkity) #2

Found the solution.

After runAction call

i set correct header in response




$response = Yii::$app->runAction('api/availability/create_availability');

Yii::$app->response->format = 'html';



so… every time i call internal api i must adjust the response format.

In this way i have a single point of failure :)

It works but this is correct way ?


(Alexander Makarov) #3

Yep. Wrap it into a method and use it.