Created
November 30, 2015 22:08
-
-
Save rmrhz/39206a194feb38fb8bd8 to your computer and use it in GitHub Desktop.
Automatically return a JSON response when `return` is invoked
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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