When running a php bin/magento setup:di:compile
you may have at some point run into the “Incorrect dependency in class” errors during the generation of the code. They look a little something like this:
This happens when you include a class in your constructor that is already available inside $context
. The solution to this problem is actually quite simple. Instead of doing something like this
public function __construct( Context $context, \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, ) { parent::__construct($context); $this->config = $scopeConfig; }
You should do the following as the ScopeConfigInterface
is available from $context
public function __construct( Context $context, ) { parent::__construct($context); $this->config = $context->getScopeConfig(); }
This will solve the above generation error and also make the code look a little neater, helping to keep the code clean and maintainable.
There are other classes available in the $context
to.
Block Context
$context->getRequest(); // \Magento\Framework\App\RequestInterface $context->getUrlBuilder(); // \Magento\Framework\UrlInterface $context->getScopeConfig(); // \Magento\Framework\App\Config\ScopeConfigInterface $context->getLogger(); // \Psr\Log\LoggerInterface $context->getEventManager(); // \Magento\Framework\Event\ManagerInterface $context->getStoreManager(); // \Magento\Store\Model\StoreManagerInterface $context->getPageConfig(); // \Magento\Framework\View\Page\Config $context->getFilesystem(); // \Magento\Framework\Filesystem $context->getViewFileSystem(); // \Magento\Framework\View\FileSystem $context->getAppState(); // \Magento\Framework\App\State $context->getCache(); // \Magento\Framework\App\CacheInterface $context->getSession(); // \Magento\Framework\Session\SessionManagerInterface $context->getInlineTranslation(); // \Magento\Framework\Translate\Inline\StateInterface $context->getEscaper(); // \Magento\Framework\Escaper $context->getLocaleDate(); // \Magento\Framework\Stdlib\DateTime\TimezoneInterface $context->getDesignPackage(); // \Magento\Framework\View\DesignInterface $context->getLayout(); // \Magento\Framework\View\LayoutInterface $context->getSidResolver(); // \Magento\Framework\Session\SidResolverInterface $context->getAssetRepository(); // \Magento\Framework\View\Asset\Repository $context->getViewConfig(); // \Magento\Framework\View\ConfigInterface $context->getCacheState(); // \Magento\Framework\App\Cache\StateInterface $context->getFilterManager(); // \Magento\Framework\Filter\FilterManager
Helper Context
$context->getRequest(); // \Magento\Framework\App\RequestInterface $context->getUrlBuilder(); // \Magento\Framework\UrlInterface $context->getScopeConfig(); // \Magento\Framework\App\Config\ScopeConfigInterface $context->getLogger(); // \Psr\Log\LoggerInterface $context->getEventManager(); // \Magento\Framework\Event\ManagerInterface $context->getModuleManager(); // \Magento\Framework\Module\Manager $context->getCacheConfig(); // \Magento\Framework\Cache\ConfigInterface $context->getHttpHeader(); // \Magento\Framework\HTTP\Header $context->getRemoteAddress(); // \Magento\Framework\HTTP\PhpEnvironment\RemoteAddress $context->getUrlEncoder(); // \Magento\Framework\Url\EncoderInterface $context->getUrlDecoder(); // \Magento\Framework\Url\DecoderInterface
Model Context
$context->getLogger(); // \Psr\Log\LoggerInterface $context->getCacheManager(); // \Magento\Framework\App\CacheInterface $context->getEventDispatcher(); // \Magento\Framework\Event\ManagerInterface $context->getAppState(); // \Magento\Framework\App\State $context->getLogger(); // \Psr\Log\LoggerInterface