- Sponsor
-
Notifications
You must be signed in to change notification settings - Fork 2
how does magento 2 handle url redirects
ProcessEight edited this page Jan 19, 2021
·
1 revision
/**
* Perform action and generate response
*
* @param RequestInterface $request
* @return ResponseInterface|\Magento\Framework\Controller\ResultInterface
* @throws \LogicException
*/
public function dispatch(RequestInterface $request)
{
\Magento\Framework\Profiler::start('routers_match');
$routingCycleCounter = 0;
$result = null;
while (!$request->isDispatched() && $routingCycleCounter++ < 100) {
/** @var \Magento\Framework\App\RouterInterface $router */
/* 1 */ foreach ($this->_routerList as $router) {
try {
/* 2 */ $actionInstance = $router->match($request);
if ($actionInstance) {
$request->setDispatched(true);
$this->response->setNoCacheHeaders();
if ($actionInstance instanceof \Magento\Framework\App\Action\AbstractAction) {
/* 7 */ $result = $actionInstance->dispatch($request);
} else {
$result = $actionInstance->execute();
}
/* 8 */ break;
}
} catch (\Magento\Framework\Exception\NotFoundException $e) {
// ... Not relevant to our discussion
}
}
}
// ... Not relevant to our discussion
/* 9 */ return $result;
}
-
\Magento\Framework\App\FrontController::dispatch
method passes the\Magento\Framework\App\RequestInterface
request object () to each routers'match
method. - One of these routers is
\Magento\UrlRewrite\Controller\Router
and the request is passed to its'match
method. - That calls
\Magento\UrlRewrite\Controller\Router::getRewrite
, which attempts to load a matching URL rewrite from the database. - If the rewrite is a 3xx type redirect, then the
request
from theFrontController
and therewrite
object are both passed to\Magento\UrlRewrite\Controller\Router::processRedirect
. -
processRedirect
then generates the target URL for the redirect -
\Magento\UrlRewrite\Controller\Router::redirect
sets the new URL to the response object and returns a new instance of the\Magento\Framework\App\Action\Redirect
class - Back in
\Magento\Framework\App\FrontController::dispatch
, theactionInstance
variable is now set to\Magento\Framework\App\Action\Redirect
and this instance of an action controller class is dispatched or executed (based on its inheritance tree - this is a backwards compatibility thing). - An instance of
\Magento\Framework\App\ResponseInterface
is returned, webreak
out of the$this->_routerList
loop and return the result object. - Finally we return the result object with the output from our redirected URL.