Skip to content

Instantly share code, notes, and snippets.

@rmrhz
Created November 30, 2015 22:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rmrhz/39206a194feb38fb8bd8 to your computer and use it in GitHub Desktop.
Save rmrhz/39206a194feb38fb8bd8 to your computer and use it in GitHub Desktop.
Automatically return a JSON response when `return` is invoked
<?php
use Phalcon\Mvc\View;
class Controller extends \Phalcon\Mvc\Controller
{
/**
*
* @return \Phalcon\Http\Response
*/
public function afterExecuteRoute($dispatcher)
{
if ( is_null($dispatcher->getReturnedValue()) ) {
return false;
}
$this->view->disableLevel([
View::LEVEL_ACTION_VIEW => true,
View::LEVEL_LAYOUT => true,
View::LEVEL_MAIN_LAYOUT => true,
View::LEVEL_AFTER_TEMPLATE => true,
View::LEVEL_BEFORE_TEMPLATE => true
]);
$this->response->setContentType('application/json', 'UTF-8');
$this->response->setContent(json_encode($dispatcher->getReturnedValue()));
return $this->response->send();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment