Router.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Cms\Controller;
  7. /**
  8. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  9. */
  10. class Router implements \Magento\Framework\App\RouterInterface
  11. {
  12. /**
  13. * @var \Magento\Framework\App\ActionFactory
  14. */
  15. protected $actionFactory;
  16. /**
  17. * Event manager
  18. *
  19. * @var \Magento\Framework\Event\ManagerInterface
  20. */
  21. protected $_eventManager;
  22. /**
  23. * Store manager
  24. *
  25. * @var \Magento\Store\Model\StoreManagerInterface
  26. */
  27. protected $_storeManager;
  28. /**
  29. * Page factory
  30. *
  31. * @var \Magento\Cms\Model\PageFactory
  32. */
  33. protected $_pageFactory;
  34. /**
  35. * Config primary
  36. *
  37. * @var \Magento\Framework\App\State
  38. */
  39. protected $_appState;
  40. /**
  41. * Url
  42. *
  43. * @var \Magento\Framework\UrlInterface
  44. */
  45. protected $_url;
  46. /**
  47. * Response
  48. *
  49. * @var \Magento\Framework\App\ResponseInterface
  50. */
  51. protected $_response;
  52. /**
  53. * @param \Magento\Framework\App\ActionFactory $actionFactory
  54. * @param \Magento\Framework\Event\ManagerInterface $eventManager
  55. * @param \Magento\Framework\UrlInterface $url
  56. * @param \Magento\Cms\Model\PageFactory $pageFactory
  57. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  58. * @param \Magento\Framework\App\ResponseInterface $response
  59. */
  60. public function __construct(
  61. \Magento\Framework\App\ActionFactory $actionFactory,
  62. \Magento\Framework\Event\ManagerInterface $eventManager,
  63. \Magento\Framework\UrlInterface $url,
  64. \Magento\Cms\Model\PageFactory $pageFactory,
  65. \Magento\Store\Model\StoreManagerInterface $storeManager,
  66. \Magento\Framework\App\ResponseInterface $response
  67. ) {
  68. $this->actionFactory = $actionFactory;
  69. $this->_eventManager = $eventManager;
  70. $this->_url = $url;
  71. $this->_pageFactory = $pageFactory;
  72. $this->_storeManager = $storeManager;
  73. $this->_response = $response;
  74. }
  75. /**
  76. * Validate and Match Cms Page and modify request
  77. *
  78. * @param \Magento\Framework\App\RequestInterface $request
  79. * @return \Magento\Framework\App\ActionInterface|null
  80. */
  81. public function match(\Magento\Framework\App\RequestInterface $request)
  82. {
  83. $identifier = trim($request->getPathInfo(), '/');
  84. $condition = new \Magento\Framework\DataObject(['identifier' => $identifier, 'continue' => true]);
  85. $this->_eventManager->dispatch(
  86. 'cms_controller_router_match_before',
  87. ['router' => $this, 'condition' => $condition]
  88. );
  89. $identifier = $condition->getIdentifier();
  90. if ($condition->getRedirectUrl()) {
  91. $this->_response->setRedirect($condition->getRedirectUrl());
  92. $request->setDispatched(true);
  93. return $this->actionFactory->create(\Magento\Framework\App\Action\Redirect::class);
  94. }
  95. if (!$condition->getContinue()) {
  96. return null;
  97. }
  98. /** @var \Magento\Cms\Model\Page $page */
  99. $page = $this->_pageFactory->create();
  100. $pageId = $page->checkIdentifier($identifier, $this->_storeManager->getStore()->getId());
  101. if (!$pageId) {
  102. return null;
  103. }
  104. $request->setModuleName('cms')->setControllerName('page')->setActionName('view')->setParam('page_id', $pageId);
  105. $request->setAlias(\Magento\Framework\Url::REWRITE_REQUEST_PATH_ALIAS, $identifier);
  106. return $this->actionFactory->create(\Magento\Framework\App\Action\Forward::class);
  107. }
  108. }