Yii Framework Forum: Class 'app\controllers\Response' not found - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Class 'app\controllers\Response' not found

#1 User is offline   victorfz 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 24
  • Joined: 20-January 14

Posted 04 March 2018 - 09:54 PM

Hello,
I try to do this example http://www.prettyscr...ng-jquery-ajax/
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?
Greetings.
0

#2 User is offline   alrazi 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,556
  • Joined: 08-August 12
  • Location:Durban, South Africa

Posted 04 March 2018 - 11:39 PM

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;

0

#3 User is offline   MetaCrawler 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 412
  • Joined: 12-February 11
  • Location:Germany

Posted 05 March 2018 - 01:59 AM

Hi,

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
0

#4 User is offline   victorfz 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 24
  • Joined: 20-January 14

Posted 05 March 2018 - 12:19 PM

Thanks,
I still had to add these two lines to the beginning of the file.
Solved.
Regards,


use yii;
use yii\web\Response;



View PostMetaCrawler, on 05 March 2018 - 01:59 AM, said:

Hi,

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

0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users