phpvisual-studio-codemagento2

VSCode not recognizing Magento paths in PHP file


VSCode is giving an "Undefined Type" error for the two \Magento\ paths. I installed two Magento extensions (Toolbox, Snippets) but it doesn't help. How do I get VSCode to recognize these paths?

class Index extends \Magento\Framework\App\Action\Action
{
protected $_pageFactory;

public function __construct(
      \Magento\Framework\App\Action\Context $context,
      \Magento\Framework\View\Result\PageFactory $pageFactory
     )
{
    $this->_pageFactory = $pageFactory;
    return parent::__construct($context);
}

public function execute()
{
    return $this->_pageFactory->create();
}
}

Thanks in advance


Solution

  • You need Magento’s source and PHP stubs in VSCode’s workspace so IntelliSense can resolve the classes.

    1. Open your Magento project root in VSCode (not just the module folder).
    2. Run composer install so vendor/ (with Magento’s framework files) exists.
    3. In VSCode, install the PHP Intelephense extension.
    4. Add "intelephense.environment.includePaths": ["vendor"] to your settings.json.
    5. This will remove the "Undefined Type" errors.