Magento Internals: Applications and Default Area code

Magento has concept of applications and application areas.

As of now, you can find 5 applications in Magento 2 and hence 5 different entry points to codebase.

  1. HTTP application ( \Magento\Framework\App\Http ) Entry point for end users. Found in pub/index.php
  2. Media Storage application ( \Magento\MediaStorage\App\Media ) (pub/get.php) This serves images that are not yet on the file system. It can resize images on the fly.
  3. Static Resource application (\Magento\Framework\App\StaticResource ) Entry point for static resources (JS, CSS, etc.). (pub/static.php)
  4. Cron (\Magento\Framework\App\Cron)
  5. Indexers (\Magento\Indexer\App\Indexer)

All of these implement \Magento\Framework\AppInterface . To my surprise console wasn’t an application in itself. It can lauch two application , cron and indexer.

HTTP application has concept of areas. You can have different dependency configurations,routing for different areas. Following is the list of areas found.

  1. admin
  2. frontend (Your regular storefront)
  3. Graphql
  4. Webapi

You can add your own areas by configuring `Magento\Framework\App\AreaList`. In pwa-studios upward-connector module does just that. Following snippet can be found in upward-connector module.

Snippet above adds a new application area named pwa. With this you can have your own folder inside etc directory named pwa. In that folder you can put your di.xml,events.xml that would apply only when current application area is pwa. Wait, but how does Magento know what area to load?

Magento looks at the request url. In tries to determine areacode based on url. If that can not be done, it will load the default areacode configured. This Logic can be found in \Magento\Framework\App\AreaList::getCodeByFrontName

See that last return statement. That’s what we are going to hack now.

If you look at the constructor signature of this class,

Notice how $this->_defaultAreaCode can be configured using type configuration in di.xml. In fact that’s what Magento does in vendor/magento/module-store/etc/di.xml

Notice how default default is configured to be frontend. Now we can change this to adminhtml so that when we just hit our domain, admin login page opens up.

When you put configuration above in di.xml of your module, the adminhtml router will be triggered. As it is, router will throw and error on following lines

If you debug this, you can fix this by adding following snippet to app/etc/env.php

After adding this to app/etc/env.php run

Then go the browser and just hit your domain and you should see something like following

Happy debugging!

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store