alexmontoanelli

a place to have some fun..

ZendFramework – Usando parâmentros nas Actions

Para quem usa o ZendFramework, em modo MVC, sabe que o modo para pegar uma váriavel  passada por GET/POST/COOKIE, deve ser realizada através dos métodos: getRequest()->getParam(‘nome_da_variavel’), do objeto Zend_Coontroller_Action.

Abaixo mostro uma implementação, que extende a classe Zend_Action para que seja usado os parâmentros no corpo da função.

Hoje você utiliza da seguinte forma:

<?php
class IndexController extends Zend_Controller_Action {

public function indexAction(){

//obterá o parâmentro GET/POST teste
 echo $this->getRequest()->getParam('teste');

}

}

A nova forma será:

<?php
class IndexController extends My_Action {

public function indexAction(string $teste){

//obterá o parâmentro GET/POST teste
 echo $teste;

}

}

Abaixo a classe My_Action.

Abraços

—————

<?php                                                                                                                                  
/**                                                                                                                                    
 * Map request parameters to action method                                                                                             
 * @author Albert Varaksin                                                                                                             
 * @licence public domain                                                                                                              
 */                                                                                                                                    
class My_Action extends Zend_Controller_Action                                                                           
{                                                                                                                                      
 /**                                                                                                                                
 * Dispatch the requested action                                                                                                   
 *                                                                                                                                 
 * @param string $action Method name of action                                                                                     
 * @return void                                                                                                                    
 */                                                                                                                                
 public function dispatch($action)                                                                                                  
 {                                                                                                                                  
 // Notify helpers of action preDispatch state                                                                                  
 $this->_helper->notifyPreDispatch();                                                                                           

 $this->preDispatch();
 if ($this->getRequest()->isDispatched()) {
 if (null === $this->_classMethods) {  
 $this->_classMethods = get_class_methods($this);
 }                                                   

 // preDispatch() didn't change the action, so we can continue
 if ($this->getInvokeArg('useCaseSensitiveActions')
|| in_array($action, $this->_classMethods)) {
 if ($this->getInvokeArg('useCaseSensitiveActions')) {                                       
 trigger_error('Using case sensitive actions without word separators is deprecated;
please do not rely on this "feature"');
 }                                                                                                                             

 $reflMethod = new Zend_Reflection_Method($this, $action);                                                                     
 $actionParams = $reflMethod->getParameters();                                                                                 
 $requestParams = $this->_request->getParams();                                                                                
 $args = array ();                                                                                                             
 foreach ($actionParams as $param)
 {
 // get parameter type
 if (($reflClass = $param->getClass()) instanceof Zend_Reflection_Class) {
 $type = $reflClass->getName();
 } else if ($param->isArray()) {
 $type = 'array';
 } else {
 $type = $param->getType();
 }

 // get passed parameter
 $name = $param->getName();
 if (isset($requestParams[$name])) {
 $value = $requestParams[$name];
 } else if ($param->isDefaultValueAvailable()) {
 $value = $param->getDefaultValue();
 $type = '';
 } else {
 $docBlock = $reflMethod->getDocblock();
 if (($tagRefl = $docBlock->getTag("require_$name"))
instanceof Zend_Reflection_Docblock_Tag) {
 $tryClass = trim($tagRefl->getDescription());
 if (class_exists($tryClass, true))
 throw new $tryClass("Missing value for argument $name");
 else
 throw new Zend_Controller_Action_Exception("Missing value for argument $name");
 }
 $value = null;
 }

 // fix value type
 $basicTypes = array(
 'int', 'integer', 'bool', 'boolean',
 'string', 'array', 'object',
 'double', 'float'
 );
 if (in_array($type, $basicTypes)) settype($value, $type);
 else if (strlen($type) && class_exists($type, true)) $value = new $type($value);

 $args[] = $value;
 }
 // dispatch the action
 call_user_func_array(array($this, $action), $args);
 } else {
 $this->__call($action, array());
 }
 $this->postDispatch();
 }

 // whats actually important here is that this action controller is
 // shutting down, regardless of dispatching; notify the helpers of this
 // state
 $this->_helper->notifyPostDispatch();
 }
}
October 17th, 2009 by alexm