Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Base.php
Go to the documentation of this file.
1 <?php
9 
17 {
21  const NO_ROUTE = 'noroute';
22 
26  protected $actionFactory;
27 
31  protected $actionInterface = \Magento\Framework\App\ActionInterface::class;
32 
36  protected $_modules = [];
37 
41  protected $_dispatchData = [];
42 
48  protected $_requiredParams = ['moduleFrontName', 'actionPath', 'actionName'];
49 
53  protected $_routeConfig;
54 
60  protected $_urlSecurityInfo;
61 
67  protected $_scopeConfig;
68 
72  protected $_url;
73 
77  protected $_storeManager;
78 
82  protected $_responseFactory;
83 
87  protected $_defaultPath;
88 
92  protected $nameBuilder;
93 
97  protected $reservedNames = ['new', 'print', 'switch', 'return'];
98 
104  protected $applyNoRoute = false;
105 
109  protected $pathPrefix = null;
110 
114  protected $actionList;
115 
119  protected $pathConfig;
120 
135  public function __construct(
136  \Magento\Framework\App\Router\ActionList $actionList,
138  \Magento\Framework\App\DefaultPathInterface $defaultPath,
139  \Magento\Framework\App\ResponseFactory $responseFactory,
140  \Magento\Framework\App\Route\ConfigInterface $routeConfig,
141  \Magento\Framework\UrlInterface $url,
142  \Magento\Framework\Code\NameBuilder $nameBuilder,
143  \Magento\Framework\App\Router\PathConfigInterface $pathConfig
144  ) {
145  $this->actionList = $actionList;
146  $this->actionFactory = $actionFactory;
147  $this->_responseFactory = $responseFactory;
148  $this->_defaultPath = $defaultPath;
149  $this->_routeConfig = $routeConfig;
150  $this->_url = $url;
151  $this->nameBuilder = $nameBuilder;
152  $this->pathConfig = $pathConfig;
153  }
154 
161  public function match(\Magento\Framework\App\RequestInterface $request)
162  {
163  $params = $this->parseRequest($request);
164 
165  return $this->matchAction($request, $params);
166  }
167 
174  protected function parseRequest(\Magento\Framework\App\RequestInterface $request)
175  {
176  $output = [];
177 
178  $path = trim($request->getPathInfo(), '/');
179 
180  $params = explode('/', $path ? $path : $this->pathConfig->getDefaultPath());
181  foreach ($this->_requiredParams as $paramName) {
182  $output[$paramName] = array_shift($params);
183  }
184 
185  for ($i = 0, $l = sizeof($params); $i < $l; $i += 2) {
186  $output['variables'][$params[$i]] = isset($params[$i + 1]) ? urldecode($params[$i + 1]) : '';
187  }
188  return $output;
189  }
190 
198  protected function matchModuleFrontName(\Magento\Framework\App\RequestInterface $request, $param)
199  {
200  // get module name
201  if ($request->getModuleName()) {
202  $moduleFrontName = $request->getModuleName();
203  } elseif (!empty($param)) {
204  $moduleFrontName = $param;
205  } else {
206  $moduleFrontName = $this->_defaultPath->getPart('module');
207  $request->setAlias(\Magento\Framework\Url::REWRITE_REQUEST_PATH_ALIAS, '');
208  }
209  if (!$moduleFrontName) {
210  return null;
211  }
212  return $moduleFrontName;
213  }
214 
222  protected function matchActionPath(\Magento\Framework\App\RequestInterface $request, $param)
223  {
224  if ($request->getControllerName()) {
225  $actionPath = $request->getControllerName();
226  } elseif (!empty($param)) {
227  $actionPath = $param;
228  } else {
229  $actionPath = $this->_defaultPath->getPart('controller');
230  $request->setAlias(
232  ltrim($request->getOriginalPathInfo(), '/')
233  );
234  }
235  return $actionPath;
236  }
237 
244  protected function getNotFoundAction($currentModuleName)
245  {
246  if (!$this->applyNoRoute) {
247  return null;
248  }
249 
250  $actionClassName = $this->getActionClassName($currentModuleName, 'noroute');
251  if (!$actionClassName || !is_subclass_of($actionClassName, $this->actionInterface)) {
252  return null;
253  }
254 
255  // instantiate action class
256  return $this->actionFactory->create($actionClassName);
257  }
258 
268  protected function matchAction(\Magento\Framework\App\RequestInterface $request, array $params)
269  {
270  $moduleFrontName = $this->matchModuleFrontName($request, $params['moduleFrontName']);
271  if (empty($moduleFrontName)) {
272  return null;
273  }
274 
278  $modules = $this->_routeConfig->getModulesByFrontName($moduleFrontName);
279 
280  if (empty($modules) === true) {
281  return null;
282  }
283 
287  $currentModuleName = null;
288  $actionPath = null;
289  $action = null;
290  $actionInstance = null;
291 
292  $actionPath = $this->matchActionPath($request, $params['actionPath']);
293  $action = $request->getActionName() ?: ($params['actionName'] ?: $this->_defaultPath->getPart('action'));
294  $this->_checkShouldBeSecure($request, '/' . $moduleFrontName . '/' . $actionPath . '/' . $action);
295 
296  foreach ($modules as $moduleName) {
297  $currentModuleName = $moduleName;
298 
299  $actionClassName = $this->actionList->get($moduleName, $this->pathPrefix, $actionPath, $action);
300  if (!$actionClassName || !is_subclass_of($actionClassName, $this->actionInterface)) {
301  continue;
302  }
303 
304  $actionInstance = $this->actionFactory->create($actionClassName);
305  break;
306  }
307 
308  if (null == $actionInstance) {
309  $actionInstance = $this->getNotFoundAction($currentModuleName);
310  if ($actionInstance === null) {
311  return null;
312  }
313  $action = self::NO_ROUTE;
314  }
315 
316  // set values only after all the checks are done
317  $request->setModuleName($moduleFrontName);
318  $request->setControllerName($actionPath);
319  $request->setActionName($action);
320  $request->setControllerModule($currentModuleName);
321  $request->setRouteName($this->_routeConfig->getRouteByFrontName($moduleFrontName));
322  if (isset($params['variables'])) {
323  $request->setParams($params['variables']);
324  }
325  return $actionInstance;
326  }
327 
335  public function getActionClassName($module, $actionPath)
336  {
337  $prefix = $this->pathPrefix ? 'Controller\\' . $this->pathPrefix : 'Controller';
338  return $this->nameBuilder->buildClassName([$module, $prefix, $actionPath]);
339  }
340 
351  protected function _checkShouldBeSecure(\Magento\Framework\App\RequestInterface $request, $path = '')
352  {
353  if ($request->getPostValue()) {
354  return;
355  }
356 
357  if ($this->pathConfig->shouldBeSecure($path) && !$request->isSecure()) {
358  $url = $this->pathConfig->getCurrentSecureUrl($request);
359  if ($this->_shouldRedirectToSecure()) {
360  $url = $this->_url->getRedirectUrl($url);
361  }
362 
363  $this->_responseFactory->create()->setRedirect($url)->sendResponse();
364  exit;
365  }
366  }
367 
373  protected function _shouldRedirectToSecure()
374  {
375  return $this->_url->getUseSession();
376  }
377 }
is_subclass_of($obj, $className)
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
getActionClassName($module, $actionPath)
Definition: Base.php:335
parseRequest(\Magento\Framework\App\RequestInterface $request)
Definition: Base.php:174
$prefix
Definition: name.phtml:25
exit
Definition: redirect.phtml:12
match(\Magento\Framework\App\RequestInterface $request)
Definition: Base.php:161
matchModuleFrontName(\Magento\Framework\App\RequestInterface $request, $param)
Definition: Base.php:198
matchAction(\Magento\Framework\App\RequestInterface $request, array $params)
Definition: Base.php:268
matchActionPath(\Magento\Framework\App\RequestInterface $request, $param)
Definition: Base.php:222
_checkShouldBeSecure(\Magento\Framework\App\RequestInterface $request, $path='')
Definition: Base.php:351
getNotFoundAction($currentModuleName)
Definition: Base.php:244
if(isset($opts->o)) if(! $usingStdout) $l
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18
$i
Definition: gallery.phtml:31
__construct(\Magento\Framework\App\Router\ActionList $actionList, \Magento\Framework\App\ActionFactory $actionFactory, \Magento\Framework\App\DefaultPathInterface $defaultPath, \Magento\Framework\App\ResponseFactory $responseFactory, \Magento\Framework\App\Route\ConfigInterface $routeConfig, \Magento\Framework\UrlInterface $url, \Magento\Framework\Code\NameBuilder $nameBuilder, \Magento\Framework\App\Router\PathConfigInterface $pathConfig)
Definition: Base.php:135