I'm using Zend framework, and most of the action controller that I hav开发者_运维问答e return XML response. In order to do this, I have to initialize context switching and provide URL suffix "?format=xml" to call each action.
Is there any way to make this default? So I don't have to add this suffix each URL?
Regards, Andree.
EXTRA EXTRA.. READ ALL ABOUT IT!
http://framework.zend.com/manual/en/zend.controller.actionhelpers.html
Have a look at ContextSwitch and AjaxContext
(Edit) Suggest you use:
In some cases, you may want to force the context used; for instance, you may only want to allow the XML context if context switching is activated. You can do so by passing the context to initContext():
$contextSwitch->initContext('xml');
What about explicitly setting the parameter format
in the init()
method of your controller?
$this->getRequest()->setParam('format', 'xml');
... there has to be a not so lazy way of doing this, though ...
Alternatively, you can use something like the following to set a default, but still allow other contexts to be set via the "format" parameter:
$ajaxContext = $this->_helper->getHelper('AjaxContext');
$currentContext = $ajaxContext->getCurrentContext();
if (empty($currentContext)) {
$ajaxContext->initContext('xml');
}
This code can be placed in your controllers' init(), which would set the default context for all actions. It can also be placed in individual actions to set the default context on a per-action basis.
Note that you can also change the parameter name to something other than "format" with this:
$ajaxContext->setContextParam('type');
Then you could call your action with '/controller/action/type/xml'.
If you do not want to have to set the param in every controller init, you can also set the param as a Global route param. In your bootstrap pull the router instance and call
$router->setGlobalParam('format', 'xml');
Or you can set the defaults in the routes you define, or possibly if using the default router anywhere in your url, after your named params /format/xml.
精彩评论