Little stuff to make json responses in phalcon
clone repo or composer require fizzka/phalcon-json-view
Customize your phalcon events manager:
use Fizz\Phalcon\JsonView\JsonView;
use Phalcon\Mvc\Dispatcher;
$di->set('dispatcher', function () use ($di) {
$eventsManager = $di->getShared('eventsManager');
$json = new JsonView();
$eventsManager->attach('dispatch:afterDispatchLoop', $json);
$dispatcher = new Dispatcher();
$dispatcher->setEventsManager($eventsManager);
return $dispatcher;
}, true);
Disable view:
use Phalcon\Mvc\View;
$di->set('view', function () {
$view = new View();
$view->disable();
return $view;
}, true);
Extend abstract class Fizz\Phalcon\JsonView\Controller;
use Fizz\Phalcon\JsonView\Controller;
class ExampleController extends Controller
{
public function indexAction()
{
return ['foo' => 'baz'];
}
}
Full-working example @see example.php
MIT