26

In most of the class's Constructors, a Context object is passed . I couldn't understand how this Context Obj works . I also noticed that sometimes this is passed to parent class's constructor like below.

public function __construct(
    \Magento\Framework\App\Action\Context $context,
    \Magento\Catalog\Model\Design $catalogDesign,
    \Magento\Catalog\Model\Session $catalogSession,
....
    parent::__construct(
        $context,
        $layoutFactory,

Can you please explain how this specific context object works ?

3 Answers 3

33

Note that there are different Context objects, in this case it's \Magento\Framework\App\Action\Context and to understand it, you should read it as "ActionContext". It represents the application context in which the action is executed. In other words, it gives you access to all objects with application state that a controller action needs, for example the registry or the request object.

The context classes don't have own functionality, they are just a container for other objects. You can see them as shortcut to not have 20 parameters in each controller action. All common parameters are merged in the context object.

4
  • how could i know which object are contained by different $context?
    – LucScu
    Jun 9, 2016 at 6:48
  • 1
    @LucaS look at their source code. You find the contained classes in the context constructor Jun 9, 2016 at 7:22
  • Would a context relate to the delcarations in di.xml? So if I was using a module's context I would get access to the di configurations? Or have I got the wrong end of the stick entirely? Oct 13, 2020 at 7:26
  • @ScottAnderson Wrong end of the stick :) Context does not have any special functionality, it's a regular object with constructor dependencies and getters. Used as container for other objects Oct 13, 2020 at 12:52
17

Context objects were introduced to isolate third party developers from changes in constructors of abstract classes.

In Magento 1 Abstract classes with a lot of "helper" behaviour were considered a convenient API for class extender. This caused huge numbers of methods and implicit dependencies in abstract classes (AbstractModel, AbstractBlock, AbstractAction)

In Magento 2 inheritance-based APIs (more precisely SPIs) are discouraged, but many legacy APIs still exist. Initially we planned to gradually remove extra behaviour from abstract classes. And to not break all extenders when we would remove some dependency from constructor, we introduced Context objects.

Current plan is to abandon inheritance-based APIs with interface-based APIs at some point.

2

Magento2 – Use Defined Methods from Context Object

When you run php bin/magento setup:di:compile command in magento2, Sometimes this type of error occurs “Incorrect dependency in class“. This error occurs when we use some class’s object in constructor which is already present in $context object.

Solution of this problem is to use methods directly from $context object.

Methods from Context object in Block.

$context->getRequest(); // return \Magento\Framework\App\RequestInterface
$context->getUrlBuilder(); // return \Magento\Framework\UrlInterface
$context->getScopeConfig(); // return \Magento\Framework\App\Config\ScopeConfigInterface
$context->getLogger(); // return \Psr\Log\LoggerInterface
$context->getEventManager(); // return \Magento\Framework\Event\ManagerInterface
$context->getStoreManager(); // return \Magento\Store\Model\StoreManagerInterface
$context->getPageConfig(); // return \Magento\Framework\View\Page\Config
$context->getFilesystem(); // return \Magento\Framework\Filesystem
$context->getViewFileSystem(); // return \Magento\Framework\View\FileSystem
$context->getAppState(); // return \Magento\Framework\App\State
$context->getCache(); // return \Magento\Framework\App\CacheInterface
$context->getSession(); // return \Magento\Framework\Session\SessionManagerInterface
$context->getInlineTranslation(); // return \Magento\Framework\Translate\Inline\StateInterface
$context->getEscaper(); // return \Magento\Framework\Escaper
$context->getLocaleDate(); // return \Magento\Framework\Stdlib\DateTime\TimezoneInterface
$context->getDesignPackage(); // return \Magento\Framework\View\DesignInterface
$context->getLayout(); // return \Magento\Framework\View\LayoutInterface
$context->getSidResolver(); // return \Magento\Framework\Session\SidResolverInterface
$context->getAssetRepository(); // return \Magento\Framework\View\Asset\Repository
$context->getViewConfig(); // return \Magento\Framework\View\ConfigInterface
$context->getCacheState(); // return \Magento\Framework\App\Cache\StateInterface
$context->getFilterManager(); // return \Magento\Framework\Filter\FilterManager

Methods from Context object in Helper.

$context->getRequest(); // return \Magento\Framework\App\RequestInterface
$context->getUrlBuilder(); // return \Magento\Framework\UrlInterface
$context->getScopeConfig(); // return \Magento\Framework\App\Config\ScopeConfigInterface
$context->getLogger(); // return \Psr\Log\LoggerInterface
$context->getEventManager(); // return \Magento\Framework\Event\ManagerInterface
$context->getModuleManager(); // return \Magento\Framework\Module\Manager
$context->getCacheConfig(); // return \Magento\Framework\Cache\ConfigInterface
$context->getHttpHeader(); // return \Magento\Framework\HTTP\Header
$context->getRemoteAddress(); // return \Magento\Framework\HTTP\PhpEnvironment\RemoteAddress
$context->getUrlEncoder(); // return \Magento\Framework\Url\EncoderInterface
$context->getUrlDecoder(); // return \Magento\Framework\Url\DecoderInterface

Methods from Context object in Model.

$context->getLogger(); // return \Psr\Log\LoggerInterface
$context->getCacheManager(); // return \Magento\Framework\App\CacheInterface
$context->getEventDispatcher(); // return \Magento\Framework\Event\ManagerInterface
$context->getAppState(); // return \Magento\Framework\App\State
$context->getLogger(); // return \Psr\Log\LoggerInterface

For example if you want to use \Magento\Store\Model\StoreManagerInterface object in block. Don’t use it like following snippet

public function __construct(
    \Magento\Backend\Block\Template\Context $context,
    \Magento\Store\Model\StoreManagerInterface $storeManager,
    array $data = []
)
{
    $this->_storeManager = $storeManager;
    parent::__construct($context, $data);
}

Use it like following snippet

public function __construct(
    \Magento\Backend\Block\Template\Context $context,
    array $data = []
)
{
    $this->_storeManager = $context->getStoreManager();
    parent::__construct($context, $data);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.