Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
FrontController.php
Go to the documentation of this file.
1 <?php
8 namespace Magento\Framework\App;
9 
14 use Magento\Framework\Message\ManagerInterface as MessageManager;
16 use Psr\Log\LoggerInterface;
17 use Magento\Framework\App\Request\Http as HttpRequest;
18 
23 {
27  protected $_routerList;
28 
32  protected $response;
33 
37  private $requestValidator;
38 
42  private $messages;
43 
47  private $logger;
48 
52  private $validatedRequest = false;
53 
61  public function __construct(
62  RouterListInterface $routerList,
64  ?RequestValidator $requestValidator = null,
65  ?MessageManager $messageManager = null,
66  ?LoggerInterface $logger = null
67  ) {
68  $this->_routerList = $routerList;
69  $this->response = $response;
70  $this->requestValidator = $requestValidator
71  ?? ObjectManager::getInstance()->get(RequestValidator::class);
72  $this->messages = $messageManager
73  ?? ObjectManager::getInstance()->get(MessageManager::class);
74  $this->logger = $logger
75  ?? ObjectManager::getInstance()->get(LoggerInterface::class);
76  }
77 
85  public function dispatch(RequestInterface $request)
86  {
87  \Magento\Framework\Profiler::start('routers_match');
88  $this->validatedRequest = false;
89  $routingCycleCounter = 0;
90  $result = null;
91  while (!$request->isDispatched() && $routingCycleCounter++ < 100) {
93  foreach ($this->_routerList as $router) {
94  try {
95  $actionInstance = $router->match($request);
96  if ($actionInstance) {
97  $result = $this->processRequest(
98  $request,
99  $actionInstance
100  );
101  break;
102  }
103  } catch (\Magento\Framework\Exception\NotFoundException $e) {
104  $request->initForward();
105  $request->setActionName('noroute');
106  $request->setDispatched(false);
107  break;
108  }
109  }
110  }
111  \Magento\Framework\Profiler::stop('routers_match');
112  if ($routingCycleCounter > 100) {
113  throw new \LogicException('Front controller reached 100 router match iterations');
114  }
115  return $result;
116  }
117 
125  private function processRequest(
126  HttpRequest $request,
127  ActionInterface $actionInstance
128  ) {
129  $request->setDispatched(true);
130  $this->response->setNoCacheHeaders();
131  $result = null;
132 
133  //Validating a request only once.
134  if (!$this->validatedRequest) {
135  try {
136  $this->requestValidator->validate(
137  $request,
138  $actionInstance
139  );
140  } catch (InvalidRequestException $exception) {
141  //Validation failed - processing validation results.
142  $this->logger->debug(
143  'Request validation failed for action "'
144  .get_class($actionInstance) .'"'
145  );
146  $result = $exception->getReplaceResult();
147  if ($messages = $exception->getMessages()) {
148  foreach ($messages as $message) {
149  $this->messages->addErrorMessage($message);
150  }
151  }
152  }
153  $this->validatedRequest = true;
154  }
155 
156  //Validation did not produce a result to replace the action's.
157  if (!$result) {
158  if ($actionInstance instanceof AbstractAction) {
159  $result = $actionInstance->dispatch($request);
160  } else {
161  $result = $actionInstance->execute();
162  }
163  }
164 
165  //handling redirect to 404
166  if ($result instanceof NotFoundException) {
167  throw $result;
168  }
169  return $result;
170  }
171 }
$message
__construct(RouterListInterface $routerList, ResponseInterface $response, ?RequestValidator $requestValidator=null, ?MessageManager $messageManager=null, ?LoggerInterface $logger=null)