XF 2.0 Proper way to return JSON

Jean-Baptiste

Formerly allewreK
#1
Hello,

To return JSON responses from my Controller, I'm using the following code :
Code:
        header('Content-Type: application/json');
        echo json_encode($data);
        die();
What is the good way to return JSON ?

Thanks
 
#3
Hello,

To return JSON responses from my Controller, I'm using the following code :
Code:
        header('Content-Type: application/json');
        echo json_encode($data);
        die();
What is the good way to return JSON ?

Thanks
i think this is bad way, in xf 1.5.x you can return json data through jsonEncodeForOutput function in ViewPublic:
Code:
class Dev_TestAddon_ViewPublic_Test extends XenForo_ViewPublic_Base
{
    public function renderJson()
    {
        return XenForo_ViewRenderer_Json::jsonEncodeForOutput($this->_params);
    }
}
So, xf 2.0 must have the similar function to return json data
 

Ralle

Active member
#5
I've been doing this:
PHP:
        $reply = $this->message('Success');
        $reply->setJsonParams(['a' => 1, 'b' => 2]);
        return $reply;
The only ugly part is that I don't care for the message.
 
#8
Sorry to pull up an old thread, but I try this:
PHP:
    public function actionTest(){

        $this->setResponseType('json');

        return $this->view('Em8er\Auth:API', '', ["foo" => "bar"]);

    }
And the response I get is this:
Code:
{"status":"ok","html":{"content":""},"visitor":{"conversations_unread":"0","alerts_unread":"0","total_unread":"0"},"debug":{"time":0.1417,"queries":4,"memory":1.72}}
I'm not getting my foo:bar view parameter coming through.

Any advice?
 
Top