proweb
8/14/2013 - 7:50 AM

Best way for JSON output in Joomla controller

Best way for JSON output in Joomla controller

class MyController extends JController
{
    function someTask()
    {
        // Get the application object.
        $app = JFactory::getApplication();

        // Get the model.
        $model = $this->getModel('MyModel');

        // Get the data from the model.
        $data = $model->getData();

        // Check for errors.
        if ($model->getError()) {
            // Do something.
        }

        // Echo the data as JSON.
        echo json_encode($data);

        // Close the application.
        $app->close();
    }
}