Action.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_View
  17. * @subpackage Helper
  18. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @version $Id$
  20. * @license http://framework.zend.com/license/new-bsd New BSD License
  21. */
  22. /** Zend_View_Helper_Abstract.php */
  23. require_once 'Zend/View/Helper/Abstract.php';
  24. /**
  25. * Helper for rendering output of a controller action
  26. *
  27. * @package Zend_View
  28. * @subpackage Helper
  29. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  30. * @license http://framework.zend.com/license/new-bsd New BSD License
  31. */
  32. class Zend_View_Helper_Action extends Zend_View_Helper_Abstract
  33. {
  34. /**
  35. * @var string
  36. */
  37. public $defaultModule;
  38. /**
  39. * @var Zend_Controller_Dispatcher_Interface
  40. */
  41. public $dispatcher;
  42. /**
  43. * @var Zend_Controller_Request_Abstract
  44. */
  45. public $request;
  46. /**
  47. * @var Zend_Controller_Response_Abstract
  48. */
  49. public $response;
  50. /**
  51. * Constructor
  52. *
  53. * Grab local copies of various MVC objects
  54. *
  55. * @return void
  56. */
  57. public function __construct()
  58. {
  59. $front = Zend_Controller_Front::getInstance();
  60. $modules = $front->getControllerDirectory();
  61. if (empty($modules)) {
  62. require_once 'Zend/View/Exception.php';
  63. throw new Zend_View_Exception('Action helper depends on valid front controller instance');
  64. }
  65. $request = $front->getRequest();
  66. $response = $front->getResponse();
  67. if (empty($request) || empty($response)) {
  68. require_once 'Zend/View/Exception.php';
  69. throw new Zend_View_Exception('Action view helper requires both a registered request and response object in the front controller instance');
  70. }
  71. $this->request = clone $request;
  72. $this->response = clone $response;
  73. $this->dispatcher = clone $front->getDispatcher();
  74. $this->defaultModule = $front->getDefaultModule();
  75. }
  76. /**
  77. * Reset object states
  78. *
  79. * @return void
  80. */
  81. public function resetObjects()
  82. {
  83. $params = $this->request->getUserParams();
  84. foreach (array_keys($params) as $key) {
  85. $this->request->setParam($key, null);
  86. }
  87. $this->response->clearBody();
  88. $this->response->clearHeaders()
  89. ->clearRawHeaders();
  90. }
  91. /**
  92. * Retrieve rendered contents of a controller action
  93. *
  94. * If the action results in a forward or redirect, returns empty string.
  95. *
  96. * @param string $action
  97. * @param string $controller
  98. * @param string $module Defaults to default module
  99. * @param array $params
  100. * @return string
  101. */
  102. public function action($action, $controller, $module = null, array $params = array())
  103. {
  104. $this->resetObjects();
  105. if (null === $module) {
  106. $module = $this->defaultModule;
  107. }
  108. // clone the view object to prevent over-writing of view variables
  109. $viewRendererObj = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
  110. Zend_Controller_Action_HelperBroker::addHelper(clone $viewRendererObj);
  111. $this->request->setParams($params)
  112. ->setModuleName($module)
  113. ->setControllerName($controller)
  114. ->setActionName($action)
  115. ->setDispatched(true);
  116. $this->dispatcher->dispatch($this->request, $this->response);
  117. // reset the viewRenderer object to it's original state
  118. Zend_Controller_Action_HelperBroker::addHelper($viewRendererObj);
  119. if (!$this->request->isDispatched()
  120. || $this->response->isRedirect())
  121. {
  122. // forwards and redirects render nothing
  123. return '';
  124. }
  125. $return = $this->response->getBody();
  126. $this->resetObjects();
  127. return $return;
  128. }
  129. /**
  130. * Clone the current View
  131. *
  132. * @return Zend_View_Interface
  133. */
  134. public function cloneView()
  135. {
  136. $view = clone $this->view;
  137. $view->clearVars();
  138. return $view;
  139. }
  140. }