Net implementation of Ajax controller in Zend Framework

I need a controller in my Zend Framework project that should only handle ajax requests.

My approach at the moment is to extend Zend_Controller_Action:

class Ht_Ajax_Controller extends Zend_Controller_Action{ public function preDispatch(){ $this->getResponse()->setHeader('Content-type', 'text/plain') ->setHeader('Cache-Control','no-cache'); $this->_helper->viewRenderer->setNoRender(true); $this->_helper->layout()->disableLayout(); } public function outputJson($data){ $this->getResponse()->setBody(json_encode($data)) ->sendResponse(); exit; } } 

Although, I know that this is not the recommended way to do this in the Zend Framework. So why am I asking how to do this on Zend?

The first thing I was thinking about was creating a controller plugin, but how can I easily register this plugin? Bootstrap is not an option, because I only need it for certain controllers. And registering it inside the controller in a very early state seems not very clean.

So how do I implement my Ajax controller? I also know that there is an advanced context switching assistant, however I think that it has too much overhead for just setting the content type, etc.

+4
source share
4 answers

markus explained the most “Zend-like way”, and his explanation is correct. But you see this too much overhead, because he skipped to show you how "json context switch" is best for your case.

See how short it is:

 //In your controller, activate json context for ANY called action in once public function init(){ $action = $this->_getParam('action'); $this->_helper->getHelper('contextSwitch') ->addActionContext($action, 'json') ->initContext(); } 

And that’s all , you don’t need any view scripts, all the variables that you assign through $this->view will be serialized into a JSON object.

Oh, and you do not want to add context to the URL? Ok, just activate it by default in your route

 routes.ajax.route = '/ajax/:action/*' routes.ajax.defaults.controller = ajax routes.ajax.defaults.action = index routes.ajax.defaults.format = json 
+4
source

You can just use

 $this->_helper->json($data); 

wherever. It disables everything that is not needed, clears the response, encodes the data in json, sends the response and sets the propper headers as a bonus. This is the most "zend-way", I suppose;)

+3
source

The content needs to be switched only when you actually call the ajax action. I think the Zend path is a good way. It is very flexible and easy to implement. In most cases, different controllers will need several ajax actions. I do not see the need or benefits of pure ajax controllers.

Each controller may have something similar in init ():

 $ajaxContext = $this->_helper->getHelper('AjaxContext'); $ajaxContext->addActionContext('some-toggle', 'html'); $ajaxContext->addActionContext('some-other-ajax-thing', 'json'); $ajaxContext->initContext(); 

An action looks like any other action. There is only one var parameter in the script view:

 <?php echo $this->response; ?> 

... and should be called actionname.ajax.phtml.

Then, if you click additional actions in your action stack, you only need to do this for queries without an ajax, for example:

 if (!$request->isXmlHttpRequest()) { //push actions on stack } 

In addition, you need to pass the format parameter along with the ajax post address, for example posturl/format/html or posturl/format/json .

+2
source

I did the same as you, I have a controller specifically for processing ajax requests, however my solution is much simpler, I use the init () function, not preDispatch.

My init () function looks like this:

 class Ajax_Controller extends Zend_Controller_Action { public function init() { $this->_helper->layout()->disableLayout(); } //the rest of the controller... } 

What is it! I have not made any other changes from the standard controller.

I will disable rendering rendering in action, if necessary, when I found, in my case I needed a view to render tables, etc. However, if you only ever return json data, then your method of disabling rendering rendering in preDispatch () is perfectly valid.

If I need to return json, I have a similar function for yours and I will turn off rendering rendering.

I used this method on 4 separate but similar projects without any problems. Having said that my ajax calls are usually pretty simple, but I don't see any problems with this method.

If you are looking for a simple, clean ajax implementation, then this may be for you.

+1
source

All Articles