code_junkie

A Generic, catch-all action in Zend Framework… can it be done

Nov 13th, 2011
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.31 KB | None | 0 0
  1. new Zend_Controller_Router_Route('index/*',
  2. array('controller' => 'index', 'action' => 'custom', 'module'=>'index')
  3.  
  4. $this->_helper->layout()->disableLayout();
  5. $this->_helper->viewRenderer->setNoRender(true);
  6.  
  7. class UDC_Controller_Router_Route_Catchall extends Zend_Controller_Router_Route_Module
  8. {
  9. private $_catchallController = 'page';
  10. private $_catchallAction = 'index';
  11. private $_paramName = 'name';
  12.  
  13. //-------------------------------------------------------------------------
  14. /*! brief takes most of the default behaviour from Zend_Controller_Router_Route_Module
  15. with the following changes:
  16. - if the path includes a valid module, then use it
  17. - if the path includes a valid controller (file_exists) then use that
  18. - otherwise use the catchall
  19. */
  20. public function match($path, $partial = false)
  21. {
  22. $this->_setRequestKeys();
  23.  
  24. $values = array();
  25. $params = array();
  26.  
  27. if (!$partial) {
  28. $path = trim($path, self::URI_DELIMITER);
  29. } else {
  30. $matchedPath = $path;
  31. }
  32.  
  33. if ($path != '') {
  34. $path = explode(self::URI_DELIMITER, $path);
  35.  
  36. if ($this->_dispatcher && $this->_dispatcher->isValidModule($path[0])) {
  37. $values[$this->_moduleKey] = array_shift($path);
  38. $this->_moduleValid = true;
  39. }
  40.  
  41. if (count($path) && !empty($path[0])) {
  42. $module = $this->_moduleValid ? $values[$this->_moduleKey] : $this->_defaults[$this->_moduleKey];
  43. $file = $this->_dispatcher->getControllerDirectory( $module ) . '/' . $this->_dispatcher->formatControllerName( $path[0] ) . '.php';
  44. if (file_exists( $file ))
  45. {
  46. $values[$this->_controllerKey] = array_shift($path);
  47. }
  48. else
  49. {
  50. $values[$this->_controllerKey] = $this->_catchallController;
  51. $values[$this->_actionKey] = $this->_catchallAction;
  52. $params[$this->_paramName] = join( self::URI_DELIMITER, $path );
  53. $path = array();
  54. }
  55. }
  56.  
  57. if (count($path) && !empty($path[0])) {
  58. $values[$this->_actionKey] = array_shift($path);
  59. }
  60.  
  61. if ($numSegs = count($path)) {
  62. for ($i = 0; $i < $numSegs; $i = $i + 2) {
  63. $key = urldecode($path[$i]);
  64. $val = isset($path[$i + 1]) ? urldecode($path[$i + 1]) : null;
  65. $params[$key] = (isset($params[$key]) ? (array_merge((array) $params[$key], array($val))): $val);
  66. }
  67. }
  68. }
  69.  
  70. if ($partial) {
  71. $this->setMatchedPath($matchedPath);
  72. }
  73.  
  74. $this->_values = $values + $params;
  75.  
  76. return $this->_values + $this->_defaults;
  77. }
  78. }
  79.  
  80. public function __call($name, $arguments)
  81. {
  82. // Render Simple HTML View
  83. }
  84.  
  85. // 1. Catch & process overloaded actions.
  86. public function __call($name, $arguments)
  87. {
  88. // 2. Provide an appropriate renderer.
  89. $this->_helper->viewRenderer->setRender('overload');
  90. // 3. Bonus: give your view script a clue about what "action" was requested.
  91. $this->view->action = $this->getFrontController()->getRequest()->getActionName();
  92. }
Add Comment
Please, Sign In to add comment