Skip to content
intermediate Phase 15 · Routing & Areas

Magento 2 Areas Concept

Understanding Magento area concept - global, frontend, adminhtml, webapi_rest, webapi_graphql, crontab and how areas affect configuration loading.

45m
0 problems
Topic Progress 0%

What Are Areas in Magento 2?

Areas in Magento 2 are logical groupings that determine which configuration and code is loaded for a specific application context. Each area represents a different way Magento processes requests.

When Magento boots, it determines which area is active based on the request type. For example:

  • A storefront page request loads the frontend area
  • An admin panel request loads the adminhtml area
  • A REST API call loads the webapi_rest area
  • A GraphQL query loads the webapi_graphql area

Areas serve two critical purposes:

1. Configuration Scope: Each module can define area-specific configuration in etc/{area}/ directories. For example, etc/frontend/routes.xml defines routes only for the frontend area, while etc/adminhtml/routes.xml defines admin routes.

2. Code Loading: Modules can declare area-specific code that only loads when that area is active. This optimizes memory usage by not loading unnecessary classes.

The area code is set early in the application lifecycle by an area resolver (like Magento\Framework\App\Area\Resolver\HttpResolver for web requests) and stored in the application state. Once set, the area determines which configuration pool is used throughout the request.

Default Magento Areas

Magento 2 ships with six default areas, each serving a distinct purpose:

global - Not a request area per se, but the base configuration scope. Contains settings shared across all areas like DI configuration, module declarations, and system configuration.

frontend - Handles the customer-facing storefront. Loads theme configuration, layout XML, JavaScript bundles, CSS, and storefront-specific di.xml settings.

adminhtml - Handles the Magento admin panel. Loads admin routes, ACL permissions, UI components, admin themes, and system configuration panels.

webapi_rest - Handles REST API requests. Loads service contracts exposed via REST, authentication configuration, and API-specific routing.

webapi_graphql - Handles GraphQL queries and mutations. Loads GraphQL schema definitions, resolvers, and GraphQL-specific type configurations.

crontab - Handles cron job execution. Loads scheduled task definitions and cron-specific module configurations.

You can list registered areas programmatically:

$areaList = $objectManager->get(
    \Magento\Framework\App\Area\AreaListInterface::class
);

foreach ($areaList->getAreas() as $areaCode => $areaConfig) {
    echo $areaCode . ': ' . $areaConfig['name'] . "\n";
}

How Areas Affect Configuration Loading

Magento's configuration loading is area-aware. When an area is activated, Magento loads configuration from specific directories.

Configuration directory structure:

app/code/Vendor/Module/etc/
├── di.xml              # Global (all areas)
├── frontend/
│   ├── di.xml          # Frontend only
│   ├── routes.xml      # Frontend routes
│   └── layout.xml      # Frontend layouts
├── adminhtml/
│   ├── di.xml          # Admin only
│   ├── routes.xml      # Admin routes
│   └── acl.xml         # Admin permissions
├── webapi_rest/
│   └── di.xml          # REST API config
├── webapi_graphql/
│   └── di.xml          # GraphQL config
└── crontab/
    └── di.xml          # Cron config

The loading process:

  1. Area code is set (e.g., frontend)
  2. Magento reads global di.xml
  3. Magento reads etc/frontend/di.xml
  4. Both are merged, with area-specific overriding global
// Magento loads config like this:
public function loadConfiguration($areaCode)
{
    // Load global config
    $this->loadModuleConfig('etc/');
    
    // Load area-specific config
    $this->loadModuleConfig('etc/' . $areaCode . '/');
    
    // Area config overrides global
    return $this->configMerger->merge(
        $this->globalConfig,
        $this->areaConfig
    );
}

This architecture means a module can define different class preferences, virtual types, and plugin configurations for different areas.

Area Code Management

The area code is managed by Magento\Framework\App\State and must be set before configuration loading begins.

// Setting area code (typically done by area resolvers)
$objectManager->get(\Magento\Framework\App\State::class)
    ->setAreaCode('frontend');

Area Resolvers are classes that automatically determine the area based on the request context. Magento uses:

  • Magento\Framework\App\Area\Resolver\HttpResolver - Sets frontend or adminhtml based on URL
  • Magento\Webapi\Model\AreaResolver - Sets webapi_rest or webapi_graphql
  • Magento\Cron\Model\AreaResolver - Sets crontab for CLI cron

You can create custom area resolvers:

<?php
namespace Vendor\Module\App\Area\Resolver;

use Magento\Framework\App\Area\ResolverInterface;
use Magento\Framework\App\RequestInterface;

class CustomResolver implements ResolverInterface
{
    public function getAreaCode(): string
    {
        // Determine area based on your logic
        return 'frontend';
    }
}

Register the resolver:

<config>
    <type name="Magento\Framework\App\Area\ResolverPool">
        <arguments>
            <argument name="resolvers" xsi:type="array">
                <item name="custom" xsi:type="object">
                    Vendor\Module\App\Area\Resolver\CustomResolver
                </item>
            </argument>
        </arguments>
    </type>
</config>

Important: Area code must be set only once per request. Attempting to set it again throws a LocalizedException. Use emulateAreaCode() for testing or background processes.

Quiz

1. Which area handles the Magento admin panel?

Question 1 options

2. How does area-specific DI configuration affect global configuration?

Question 2 options

3. Which class manages the current area code in Magento?

Question 3 options

Flashcards

Question

What are the 6 default Magento 2 areas?

Answer

global, frontend, adminhtml, webapi_rest, webapi_graphql, crontab

Question

Where do area-specific XML files go?

Answer

etc/{areaCode}/ directory within each module

Question

What happens if you try to set the area code twice?

Answer

Magento throws a LocalizedException

Question

Which area resolver handles storefront requests?

Answer

Magento\Framework\App\Area\Resolver\HttpResolver

Revision Notes

Key Takeaways

  • 1. Areas are logical groupings that scope configuration and code loading
  • 2. Magento has 6 default areas: global, frontend, adminhtml, webapi_rest, webapi_graphql, crontab
  • 3. Area-specific XML files go in etc/{areaCode}/ within each module
  • 4. Area config is merged with global config, taking precedence
  • 5. Area code is managed by Magento\Framework\App\State
  • 6. Custom area resolvers can be created and registered via DI

Interview Tips

  • List all 6 Magento 2 areas and explain what each handles
  • Explain how a module can behave differently in frontend vs adminhtml
  • Describe the configuration merging process for area-specific XML
  • Discuss when you would create a custom area resolver
  • Explain why areas optimize performance by reducing loaded code

Cheat Sheet

Magento 2 Areas Cheat Sheet

Default Areas:

  • global - Shared across all areas
  • frontend - Storefront
  • adminhtml - Admin panel
  • webapi_rest - REST API
  • webapi_graphql - GraphQL API
  • crontab - Cron jobs

Area Config Path: etc/{areaCode}/

Setting Area Code:

$state->setAreaCode('frontend');

Emulate Area Code:

$state->emulateAreaCode('adminhtml', function() { ... });

Area Resolution Order:

  1. Check HTTP request URL
  2. Check CLI command context
  3. Fall back to default area