Request flow processing

URLs in Magento have the format of <AreaFrontName>/<VendorName>/<ModuleName>/<ControllerName>/<ActionName>, but this does not mean that we actually use the area, vendor, or module name in the URL any time we wish to access a certain controller. For example, the area for a request is defined by the first request path segment, such as admin for adminhtml area, and none for frontend area.

We use the router class to assign a URL to a corresponding controller and its action. The router's match method finds a matching controller, which is determined by an incoming request.

Conceptually, creating a new router is as simple as doing the following:

  1. Inject the new item under the routerList argument of the MagentoFrameworkAppRouterList type via the di.xml file.
  2. Create a router file (by using the match method, which implements MagentoFrameworkAppRouterInterface).
  3. Return an instance of MagentoFrameworkAppActionInterface.

By doing a lookup for the name="routerList" string across all of the <MAGENTO_DIR> di.xml files, we can see the following router definitions:

  • MagentoRobotsControllerRouter (robots)
  • MagentoCmsControllerRouter (cms)
  • MagentoUrlRewriteControllerRouter (urlrewrite)
  • MagentoFrameworkAppRouterBase (standard)
  • MagentoFrameworkAppRouterDefaultRouter (default)
  • MagentoBackendAppRouter (admin)

Let's take a closer look at the robots router under <MAGENTO_DIR>/module-robots. etc/frontend/di.xml injects the new item under the routerList argument as follows:

<type name="MagentoFrameworkAppRouterList">
<arguments>
<argument name="routerList" xsi:type="array">
<item name="robots" xsi:type="array">
<item name="class" xsi:type="string">MagentoRobotsControllerRouter</item>
<item name="disable" xsi:type="boolean">false</item>
<item name="sortOrder" xsi:type="string">10</item>
</item>
</argument>
</arguments>
</type>

The MagentoRobotsControllerRouter class has been further defined as per the following partial extract:

class Router implements MagentoFrameworkAppRouterInterface {
// MagentoFrameworkAppActionFactory
private $actionFactory;
// MagentoFrameworkAppRouterActionList
private $actionList;
// MagentoFrameworkAppRouteConfigInterface
private $routeConfig;

public function match(MagentoFrameworkAppRequestInterface $request) {
$identifier = trim($request->getPathInfo(), '/'),
if ($identifier !== 'robots.txt') {
return null;
}

$modules = $this->routeConfig->getModulesByFrontName('robots'),
if (empty($modules)) {
return null;
}

$actionClassName = $this->actionList->get($modules[0], null, 'index', 'index'),
$actionInstance = $this->actionFactory->create($actionClassName);
return $actionInstance;
}
}

The match method checks if the robots.txt file was requested and returns the instance of the matched MagentoFrameworkAppActionInterface type. By following this simple implementation, we can easily create the route of our own.

Conceptually, creating a new controller is as simple as doing the following:

  1. Register a route via router.xml.
  2. Create an abstract controller file (as an abstract class, which extends MagentoFrameworkAppActionAction).
  1. Create an action controller file (which extends the main controller file with the execute method, and implements MagentoFrameworkAppActionInterface).
  2. Return an instance of MagentoFrameworkControllerResultInterface.
The separation of the controller into the main and action controller files is not a technical requirement, but rather a recommended organizational one. Magento does this across the majority of its modules.

By doing a lookup for the <route string across the <MAGENTO_DIR> routes.xml files, we can see that Magento uses hundreds of route definitions, which are spread across its modules. Each route represents one controller.

Let's take a closer look at one of Magento's controllers, <MAGENTO_DIR>/module-customer, which maps to the http://magelicious.loc/customer/address/form URL. The route itself is registered via frontend/di.xml under the standard router with a customer ID and a customer frontName, as follows:

<router id="standard">
<route id="customer" frontName="customer">
<module name="Magento_Customer" />
</route>
</router>

The abstract controller file Controller/Address.php is defined partially as follows:

abstract class Address extends MagentoFrameworkAppActionAction {
// The rest of the code...
}

The abstract controller is where we want to add functionality and dependencies that are shared across all of the child action controllers.

We can further see three different action controllers defined within the subdirectory which has the same name as the abstract class. The Controller/Address directory contains six action controllers: Delete.php, Edit.php, Form.php, FormPost.php, Index.php, and NewAction.php. Let's take a closer look at the following partial Form.php file's content:

class Form extends MagentoCustomerControllerAddress {
public function execute() {
/** @var MagentoFrameworkViewResultPage $resultPage */
$resultPage = $this->resultPageFactory->create();
$navigationBlock = $resultPage->getLayout()->getBlock('customer_account_navigation'),
if ($navigationBlock) {
$navigationBlock->setActive('customer/address'),
}
return $resultPage;
}
}

The example here uses the create method of the injected MagentoFrameworkViewResultPageFactory type to create a new page result. The various types of controller results can be found within the <MAGENTO_DIR>/framework directory:

  • MagentoFrameworkControllerResultJson
  • MagentoFrameworkControllerResultRaw
  • MagentoFrameworkControllerResultRedirect
  • MagentoFrameworkControllerResultForward
  • MagentoFrameworkViewResultLayout
  • MagentoFrameworkViewResultPage

We can take the unified way of creating result instances by using the create method of MagentoFrameworkControllerResultFactory. The ResultFactory defines the TYPE_* constant for each of the mentioned controller result types:

const TYPE_JSON = 'json';
const TYPE_RAW = 'raw';
const TYPE_REDIRECT = 'redirect';
const TYPE_FORWARD = 'forward';
const TYPE_LAYOUT = 'layout';
const TYPE_PAGE = 'page';

Keeping these constants in mind, we can easily write our action controller code as follows:

$resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
$resultRedirect->setPath('adminhtml/*/index'),
return $resultRedirect;
A quick lookup for the $this->resultFactory-> create string, across the entire <MAGENTO_DIR> directory, can give us lots of examples of how to use the ResultFactory for our own code.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.252.140