Ver Mensaje Individual
  #8 (permalink)  
Antiguo 26/05/2011, 12:52
Avatar de _ssx
_ssx
 
Fecha de Ingreso: mayo-2003
Ubicación: mX
Mensajes: 683
Antigüedad: 20 años, 11 meses
Puntos: 60
Respuesta: Automatizar routes en ZF

Bueno pues al final del día opte por hacer un objeto Routes que lea un XML y setee las rutas en el bootstrap

Código PHP:
<?php

class Routes extends DOMDocument {

    protected 
$_source;
    protected 
$_filePath;
    private 
$_routes;

    public function 
__construct($filename) {
        
$this->_filePath $filename;
        
parent::__construct($version$encoding);
        
$this->formatOutput true;
        
$this->preservWhiteSpace true;
        
$this->load($filename);
    }

    public function 
load($filename) {
        
parent::load($filename);
        
$this->parse();
    }

    public function 
add($id$name) {
        
$padre $this->createElement("route");
        
$padre->setAttribute('xml:id'$id);
        
$inner $this->createElement('cliente'$name);
        
$this->firstChild->appendChild($padre);
        
$padre->appendChild($inner);
        
$this->save($this->_filePath);
    }

    public function 
remove($id) {
        
$elemento $this->getElementById($id);
        if(
is_object($elemento)){
            
$this->deleteNode($elemento);
            return 
$this->save($this->_filePath);
        }else{
            return 
false;
        }
    }

    public function 
parse() {
        if (
$this->firstChild->hasChildNodes()) {
            foreach (
$this->firstChild->childNodes as $ruta) {
                if (
trim($ruta->nodeValue) != '')
                    
$this->_routes[] = $ruta->nodeValue;
            }
        }
    }

    function 
deleteNode($node) {
        
$this->deleteChildren($node);
        
$parent $node->parentNode;
        
$oldnode $parent->removeChild($node);
    }

    function 
deleteChildren($node) {
        while (isset(
$node->firstChild)) {
            
$this->deleteChildren($node->firstChild);
            
$node->removeChild($node->firstChild);
        }
    }

    public function 
get() {
        return 
$this->_routes;
    }

}
Y en un metodo init del Bootstrap

Código PHP:
$routesXml = new Routes(Base::getConfig()->xml->routes);
if(
count($routesXml->get())>0)
foreach(
$routesXml->get() as $cliente){
    
$route = new Zend_Controller_Router_Route_Static
    
$cliente
    array(
'controller' => 'portal''action' => 'index''cliente' =>$cliente)
); 
    
$router->addRoute($cliente$route); 

gracias :D compas
__________________
Escribe tu código de forma que refleje, y saque a relucir,lo mejor de tu carácter personal
www.oscararzola.com/blog
Principios de un programador