3

I'm using Zend framework, and most of the action controller that I have 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.

1
  • Did you find a less hack solution? Commented Jan 28, 2011 at 13:44

4 Answers 4

7

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');

Sign up to request clarification or add additional context in comments.

3 Comments

Yes, I do read about that before asking this question. But I can't find what I want there: to remove the need to add URL suffix "?format=xml" for each action to respond using XML. If you find the information that I need in the documentation, I would be glad if you could point out which one. Thanks! =)
Thanks for the clarification! But still it does not work. I still have to use "?format=xml", otherwise the context is not changing.
Now it's working. I have to do both initContext('xml') and addActionContext() to get it working.
3

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 ...

3 Comments

Yep it's working. Thanks! Still hoping if there's any less hacky solution though ;p
Hi, i have a different situation... i want such that the url must be: format.xml or abc.xml, but the output rendered must be in xml format. What i can do to achieve this?
I got the xml output after i added a header: content-type: text/xml in the view phtml page.
0

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'.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.