Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Router.php
Go to the documentation of this file.
1 <?php
7 
12 use Magento\Framework\App\Request\Http as HttpRequest;
17 
24 {
28  protected $actionFactory;
29 
33  protected $url;
34 
38  protected $storeManager;
39 
43  protected $response;
44 
48  protected $urlFinder;
49 
57  public function __construct(
58  \Magento\Framework\App\ActionFactory $actionFactory,
60  \Magento\Store\Model\StoreManagerInterface $storeManager,
61  \Magento\Framework\App\ResponseInterface $response,
63  ) {
64  $this->actionFactory = $actionFactory;
65  $this->url = $url;
66  $this->storeManager = $storeManager;
67  $this->response = $response;
68  $this->urlFinder = $urlFinder;
69  }
70 
78  public function match(RequestInterface $request)
79  {
80  $rewrite = $this->getRewrite(
81  $request->getPathInfo(),
82  $this->storeManager->getStore()->getId()
83  );
84 
85  if ($rewrite === null) {
86  //No rewrite rule matching current URl found, continuing with
87  //processing of this URL.
88  return null;
89  }
90  if ($rewrite->getRedirectType()) {
91  //Rule requires the request to be redirected to another URL
92  //and cannot be processed further.
93  return $this->processRedirect($request, $rewrite);
94  }
95  //Rule provides actual URL that can be processed by a controller.
96  $request->setAlias(
98  $rewrite->getRequestPath()
99  );
100  $request->setPathInfo('/' . $rewrite->getTargetPath());
101  return $this->actionFactory->create(
102  \Magento\Framework\App\Action\Forward::class
103  );
104  }
105 
112  protected function processRedirect($request, $rewrite)
113  {
114  $target = $rewrite->getTargetPath();
115  if ($rewrite->getEntityType() !== Rewrite::ENTITY_TYPE_CUSTOM
116  || ($prefix = substr($target, 0, 6)) !== 'http:/' && $prefix !== 'https:'
117  ) {
118  $target = $this->url->getUrl('', ['_direct' => $target]);
119  }
120  return $this->redirect($request, $target, $rewrite->getRedirectType());
121  }
122 
129  protected function redirect($request, $url, $code)
130  {
131  $this->response->setRedirect($url, $code);
132  $request->setDispatched(true);
133 
134  return $this->actionFactory->create(Redirect::class);
135  }
136 
142  protected function getRewrite($requestPath, $storeId)
143  {
144  return $this->urlFinder->findOneByData([
145  UrlRewrite::REQUEST_PATH => ltrim($requestPath, '/'),
147  ]);
148  }
149 }
$target
Definition: skip.phtml:8
processRedirect($request, $rewrite)
Definition: Router.php:112
$prefix
Definition: name.phtml:25
match(RequestInterface $request)
Definition: Router.php:78
redirect($request, $url, $code)
Definition: Router.php:129
__construct(\Magento\Framework\App\ActionFactory $actionFactory, UrlInterface $url, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Framework\App\ResponseInterface $response, UrlFinderInterface $urlFinder)
Definition: Router.php:57
getRewrite($requestPath, $storeId)
Definition: Router.php:142
$code
Definition: info.phtml:12