Welcome, Guest. Register Now!
   
Mark Forums Read Mark Forums Read Mark Forums Read


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-21-2008, 04:47 AM
Junior Member
 
Join Date: May 2008
Posts: 3
Default Choosing suffix depending on request

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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 07-27-2008, 05:07 AM
Junior Member
 
Join Date: May 2008
Posts: 3
Default

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
index.php:
PHP Code:
<?php
try {
    require 
"../init.php";
    
$objConfig Config::getInstance("paths");
    
$objFrontController Zend_Controller_Front::getInstance();
    
//$objFrontController->throwExceptions(true);
    
$objFrontController->addModuleDirectory($objConfig->get("modules"));
    
    
$objRequest = new Zend_Controller_Request_Http();
    if (
preg_match("#^(.*)\.([a-z]{2,4})$#"$_SERVER["REQUEST_URI"], $arrMatch)) {
        
$strRequestUri $arrMatch[1];
        
$strSuffix $arrMatch[2];
        
$objRequest->setRequestUri($strRequestUri);
    } else {
        
$strSuffix "html";
    }
    
$objRequest->setParam("suffix"$strSuffix);
    
$objFrontController->dispatch($objRequest);
} catch (
Exception $objException) {
    echo 
get_class($objException), "<br />",
         
$objException->getMessage(), "<br />",
         
nl2br($objException->getTraceAsString());
}
?>

I'm using an AbstractController from which all controllers inherit:
PHP Code:
<?php
abstract class AbstractController extends Zend_Controller_Action {
    
// ..
    
    
public function preDispatch() {
        
// ..
        
$this->setupView();
    }

    
// ..
    
    
protected function setupView() {
        
$this->view->strictVars(true);
        
$this->view->setEncoding("utf-8");
        
        
$objLayout Zend_Layout::startMvc(array());
        
$objLayout->setViewSuffix($this->_getParam("suffix"));
        
//$objLayout->setView($this->view);
          //  "viewSuffix" => "phtml"));
        //$objLayout->setLayout("layout");

        
$objViewRenderer Zend_Controller_Action_HelperBroker::getExistingHelper("ViewRenderer");
        
$objViewRenderer->setViewSuffix($this->_getParam("suffix"));
    }
}
?>
I had some trouble with the ViewRenderer, because I tried to add a new instance instead of the one already available as shown above.

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"}}
using Zend_View_Helper_Json


index.xml
Code:
<?xml version="1.0" encoding="utf-8"?>
<response><title>hero</title></response>
using custom view helper and PHP's DOM-Library:
PHP Code:
<?php
class Zend_View_Helper_XmlResponse extends CustomViewHelper {
    protected 
$arrPublishedVars;
    
    public function 
xmlResponse(array $arrPublishedVars) {
        
$this->arrPublishedVars $arrPublishedVars;
        return 
$this;
    }
    
    public function 
getRender() {
        
$this->disableLayout();
        
$objDocument = new DomDocument("1.0""utf-8");
        
$objRoot $objDocument->createElement("response");
        
$objDocument->appendChild($objRoot);
        foreach (
$this->arrPublishedVars as $strVar => $mixValue) {
            
$strVar strtolower($strVar);
            
$objNode $objDocument->createElement($strVar$mixValue);
            
$objRoot->appendChild($objNode);
        }
        
$objResponse Zend_Controller_Front::getInstance()->getResponse();
        
$objResponse->setHeader('Content-Type''application/xml');
        
$objResponse->sendResponse();
        return 
$objDocument->saveXml();
    }
}

/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:
<?php
class IndexController extends AbstractController {
    public function 
indexAction() {
        
$strTitle "hero";
        
$this->publishVars(get_defined_vars());
    }
}

Last edited by Chriz : 07-27-2008 at 05:10 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 07-28-2008, 12:27 AM
Member
 
Join Date: Jul 2008
Posts: 36
Default

I think what you're trying to achieve can be handled by the ContextSwitch action helper: Zend Framework: Documentation
__________________
Brenton Alker
Brisbane, Australia

http://blog.tekerson.com/
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT. The time now is 11:16 PM.