|
|||
|
Hi,
I want to provide different views for my visitors, depending on their request. Example request: "/example/help.json" So the extension (".json") should be ignored for the dispatch, so that the request goes to ExampleController::helpAction() and "something" should set the suffix for my views and layouts to "json". The question is whats the best way, and is it possible? (using Router, preDispatch, modRewrite?) I mean that's the whole sense of views, making it possible to switch them on-the-fly if another output type is requested (html, json, xml, pdf, txt, ..) but I couldn't figure out how to do that with Zend MVC. I don't want to use parameters here, because in my opinion a file extension is more intuitional and looks nicer. Thanks in advance for any suggestion, Chriz |
|
|||
|
I solved it through my bootstrap file (the regexp didn't work for mod_rewrite, because I always got "php" as extension instead of the one set in the request uri):
.htaccess: Code:
RewriteEngine On RewriteRule ^.*$ index.php PHP Code:
I'm using an AbstractController from which all controllers inherit: PHP Code:
With some helpers for JSON and self-written XML helpers, I can render my view variables now in JSON, XML, HTML (standard) and TXT (which can be used for debugging, so you see all published variables). /index /index.html Code:
<html> <body bgcolor="red"> layout open <h1>hero</h1>layout close </body> </html> /index.json Code:
{"Response":{"Title":"hero"}}
index.xml Code:
<?xml version="1.0" encoding="utf-8"?> <response><title>hero</title></response> PHP Code:
/index.txt Code:
This is for debug purposes or just for fun: value `Title` is `hero` This "Title" = "hero" is provided by just one IndexController::indexAction() method: PHP Code:
Last edited by Chriz : 07-27-2008 at 05:10 AM. |
|
|||
|
I think what you're trying to achieve can be handled by the ContextSwitch action helper: Zend Framework: Documentation
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|