Last active
April 15, 2019 15:07
-
-
Save raplos/6560810 to your computer and use it in GitHub Desktop.
ajax-answer for ajax-request in phalcon base controller
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
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