Zend ajax controller - testing if ajax, turning off layout and disabling view file


/ Published in: PHP
Save to your folder(s)

Example controller used for AJAX requests - Disables layout, shows how to sent an action to not render a view (setNoRender) and adds some security checks.


Copy this code and paste it in your HTML
  1. <?php
  2.  
  3. /******************************************
  4. App Async Controller for AJAX functionality
  5.  
  6. Module: App
  7. Controller: Async
  8.  
  9.  
  10. ******************************************/
  11.  
  12. class App_AsyncController extends Zend_Controller_Action {
  13.  
  14. public function init() {
  15. //Set no views to render in any action
  16. //You may wish to change this if you want to use a view file
  17. $this->getHelper('viewRenderer')->setNoRender();
  18.  
  19. //Ensure this is an AJAX request
  20. if(!$this->_request->isXmlHttpRequest()) {
  21. die(); //You may want to throw an Exception instead
  22. }
  23. }
  24.  
  25. public function someAction() {
  26. //Disable the layout.
  27. $this->getHelper('layout')->disableLayout();
  28. }
  29. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.