简体   繁体   中英

CakePHP how to set Content type header in appController?

I would like to have shared function for all extended controllers which print give json object as response.

I placed this shared function into the "AppContoller.php" as static function.

Problem is that i cannot access to this-> in AppController.

Question is:

How can i solve it?

Here is the code of the function:

public static function printJsonOutput($responseData)
{
    $this->RequestHandler->respondAs('json');
    $this->autoRender = false;
    echo json_encode($responseData);
}

Thanks for any help.

Read the book about json and XML views .

What you want to do is explained there in great detail, with code examples and shown the right way to do it.

What you do is wrong and like @Nunser already pointed out you don't have access to $this in a static function. You might want to do some OOP tutorial for php first?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM