Class 'app\controllers\Response' not found


I try to do this example

public function actionAjax() {

    Yii::$app->response->format = Response::FORMAT_JSON;

    $post = Yii:: $app->request->post();

    $data = [];

    if (Yii::$app->request->isAjax) {

        // do your data processing here


        // set response data

        if (success) {

            $data = ['sucess' => true, /* rest of the data */];


        else {

            $data = ['success' => false, 'error' => 'Some error message'];


        return $data;



but it gives me the following error

<< Class ‘app \ controllers \ Response’ not found >>

I have tried with

use yii \ web \ Response

Yii :: $ app-> response-> format = use yii \ web \ Response :: FORMAT_JSON;

use yii \ helpers \ Json;

And several other things but I always get an error

Where this error?

Do I have to install something with composer?


no you don’t need install anything just reference the Response class with proper namespace like so

// this should fix it

Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;


I don’t know if this is actually how you did it,

but you have to use namespaces without the space in between.

And the use statement should be right at the beginning of the file.

use yii\web\Response;

Best Regards


I still had to add these two lines to the beginning of the file.



use yii;

use yii\web\Response;