Quantcast
Channel: Webkul Blog
Viewing all articles
Browse latest Browse all 5490

Magento2 – Use Defined Methods from Context Object

$
0
0

Magneto-Code-Snippet

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);
    }

That all for context object. For any query comment below.


Viewing all articles
Browse latest Browse all 5490

Trending Articles