Skip to content

Instantly share code, notes, and snippets.

@raplos
Last active April 15, 2019 15:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save raplos/6560810 to your computer and use it in GitHub Desktop.
Save raplos/6560810 to your computer and use it in GitHub Desktop.
ajax-answer for ajax-request in phalcon base controller
public function afterExecuteRoute($dispatcher) {
$is_ajax = isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest';
if ($is_ajax) {
$this->view->disableLevel(\Phalcon\Mvc\View::LEVEL_MAIN_LAYOUT);
$eventsManager = new \Phalcon\Events\Manager();
$eventsManager->attach('view:afterRender', function ($event, $view) {
$view->setContent(json_encode(array(
'content' => $view->getContent(),
'title' => \Phalcon\Tag::getTitle(false)
)));
});
$this->view->setEventsManager($eventsManager);
$this->response->setHeader('Content-Type', 'text/plain');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment