View Single Post
  #2 (permalink)  
Old 07-27-2008, 05:07 AM
Chriz Chriz is offline
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.
Reply With Quote