Action.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\App\Action;
  7. use Magento\Framework\App\RequestInterface;
  8. use Magento\Framework\App\ResponseInterface;
  9. use Magento\Framework\Controller\ResultInterface;
  10. use Magento\Framework\Exception\NotFoundException;
  11. /**
  12. * Extend from this class to create actions controllers in frontend area of your application.
  13. * It contains standard action behavior (event dispatching, flag checks)
  14. * Action classes that do not extend from this class will lose this behavior and might not function correctly
  15. *
  16. * TODO: Remove this class. Allow implementation of Action Controllers by just implementing Action Interface.
  17. *
  18. * @api
  19. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  20. * @SuppressWarnings(PHPMD.NumberOfChildren)
  21. * @since 100.0.2
  22. */
  23. abstract class Action extends AbstractAction
  24. {
  25. /**
  26. * @var \Magento\Framework\ObjectManagerInterface
  27. */
  28. protected $_objectManager;
  29. /**
  30. * Namespace for session.
  31. * Should be defined for proper working session.
  32. *
  33. * @var string
  34. */
  35. protected $_sessionNamespace;
  36. /**
  37. * @var \Magento\Framework\Event\ManagerInterface
  38. */
  39. protected $_eventManager;
  40. /**
  41. * @var \Magento\Framework\App\ActionFlag
  42. */
  43. protected $_actionFlag;
  44. /**
  45. * @var \Magento\Framework\App\Response\RedirectInterface
  46. */
  47. protected $_redirect;
  48. /**
  49. * @var \Magento\Framework\App\ViewInterface
  50. */
  51. protected $_view;
  52. /**
  53. * @var \Magento\Framework\UrlInterface
  54. */
  55. protected $_url;
  56. /**
  57. * @var \Magento\Framework\Message\ManagerInterface
  58. */
  59. protected $messageManager;
  60. /**
  61. * @param Context $context
  62. */
  63. public function __construct(Context $context)
  64. {
  65. parent::__construct($context);
  66. $this->_objectManager = $context->getObjectManager();
  67. $this->_eventManager = $context->getEventManager();
  68. $this->_url = $context->getUrl();
  69. $this->_actionFlag = $context->getActionFlag();
  70. $this->_redirect = $context->getRedirect();
  71. $this->_view = $context->getView();
  72. $this->messageManager = $context->getMessageManager();
  73. }
  74. /**
  75. * Dispatch request
  76. *
  77. * @param RequestInterface $request
  78. * @return ResponseInterface
  79. * @throws NotFoundException
  80. */
  81. public function dispatch(RequestInterface $request)
  82. {
  83. $this->_request = $request;
  84. $profilerKey = 'CONTROLLER_ACTION:' . $request->getFullActionName();
  85. $eventParameters = ['controller_action' => $this, 'request' => $request];
  86. $this->_eventManager->dispatch('controller_action_predispatch', $eventParameters);
  87. $this->_eventManager->dispatch('controller_action_predispatch_' . $request->getRouteName(), $eventParameters);
  88. $this->_eventManager->dispatch(
  89. 'controller_action_predispatch_' . $request->getFullActionName(),
  90. $eventParameters
  91. );
  92. \Magento\Framework\Profiler::start($profilerKey);
  93. $result = null;
  94. if ($request->isDispatched() && !$this->_actionFlag->get('', self::FLAG_NO_DISPATCH)) {
  95. \Magento\Framework\Profiler::start('action_body');
  96. $result = $this->execute();
  97. \Magento\Framework\Profiler::start('postdispatch');
  98. if (!$this->_actionFlag->get('', self::FLAG_NO_POST_DISPATCH)) {
  99. $this->_eventManager->dispatch(
  100. 'controller_action_postdispatch_' . $request->getFullActionName(),
  101. $eventParameters
  102. );
  103. $this->_eventManager->dispatch(
  104. 'controller_action_postdispatch_' . $request->getRouteName(),
  105. $eventParameters
  106. );
  107. $this->_eventManager->dispatch('controller_action_postdispatch', $eventParameters);
  108. }
  109. \Magento\Framework\Profiler::stop('postdispatch');
  110. \Magento\Framework\Profiler::stop('action_body');
  111. }
  112. \Magento\Framework\Profiler::stop($profilerKey);
  113. return $result ?: $this->_response;
  114. }
  115. /**
  116. * Throw control to different action (control and module if was specified).
  117. *
  118. * @param string $action
  119. * @param string|null $controller
  120. * @param string|null $module
  121. * @param array|null $params
  122. * @return void
  123. */
  124. protected function _forward($action, $controller = null, $module = null, array $params = null)
  125. {
  126. $request = $this->getRequest();
  127. $request->initForward();
  128. if (isset($params)) {
  129. $request->setParams($params);
  130. }
  131. if (isset($controller)) {
  132. $request->setControllerName($controller);
  133. // Module should only be reset if controller has been specified
  134. if (isset($module)) {
  135. $request->setModuleName($module);
  136. }
  137. }
  138. $request->setActionName($action);
  139. $request->setDispatched(false);
  140. }
  141. /**
  142. * Set redirect into response
  143. *
  144. * @param string $path
  145. * @param array $arguments
  146. * @return ResponseInterface
  147. */
  148. protected function _redirect($path, $arguments = [])
  149. {
  150. $this->_redirect->redirect($this->getResponse(), $path, $arguments);
  151. return $this->getResponse();
  152. }
  153. /**
  154. * @return \Magento\Framework\App\ActionFlag
  155. */
  156. public function getActionFlag()
  157. {
  158. return $this->_actionFlag;
  159. }
  160. }