可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I need a controller in my Zend Framework project, which should handle only ajax requests.
My approach at the moment is to extend the 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 this isn't the recommended way to do so in Zend Framework. So that's why I am asking, how to make this in Zend's way ?
The first thing I thought about was to make a controller plugin, but how can I register this plugin easily? The bootstrap is no option because I need this only for specific controllers. And to register it inside the controller in a very early state seems not very clean.
So how should I implement my Ajax Controller? I also know there is the extended Context switching helper ,however I think that way has too much overhead for just setting the content type etc.
回答1:
markus explained the most "Zend Like" way and his explanation is correct. But you see it as too much overhead because he missed to show you how much the "json context switch" is the best for your case.
Look 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 thats all, you don't need any view scripts, all the variables you assign via $this->view
will be serialized into a JSON object.
Oh and you don't want to add the context in the urls? 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
回答2:
You can simply use
$this->_helper->json($data);
wherever you like. It disables everything not needed, clears the response, encode data to json, sends the response and set propper headers as a bonus. This is the most "zend-way" there is I guess ;)
回答3:
The content need only be switched when you're actually calling an ajax action. I think the Zend way is a good way. It is very flexible and easy to implement. In most cases, different Controllers will have a need for a couple of ajax actions. I don't see the need or the advantage of a pure ajax controller.
Every controller can have something like this in the init():
$ajaxContext = $this->_helper->getHelper('AjaxContext');
$ajaxContext->addActionContext('some-toggle', 'html');
$ajaxContext->addActionContext('some-other-ajax-thing', 'json');
$ajaxContext->initContext();
The action looks like every other action. The view script has just one var like:
<?php echo $this->response; ?>
... and has to be called actionname.ajax.phtml.
Then, if you're pushing additional actions on your action stack, you need to do that only for non-ajax requests like so:
if (!$request->isXmlHttpRequest())
{
//push actions on stack
}
Additionally, you have to pass a format param along with the ajax post url like posturl/format/html
or posturl/format/json
.
回答4:
I have done much the same as you, I have a controller specifically for handling ajax requests, however my solution is much simpler, I have use the init() function rather than 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...
}
That's it! I have made no other changes from a standard controller.
I disable view rendering in the action if required as 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 view rendering in preDispatch() is perfectly valid.
If I need to return json I have a similar function to yours and I disable view rendering.
I have used this method on 4 seperate, but similar, projects now without any issues. Having said that my ajax calls are usually pretty simple, but I don't see any issues with this method.
If you are looking for a simple, clean ajax implementation, then this may be an option for you.