Magento – Module works when in app/code but not when in vendor folder

blockserrormagento2module

Seem to have quite an odd issue. I've got a custom module that essentially is just a class which extends a Template.

When I install it using composer; it installs the module into the vendor folder. However, when trying to render a block using the class of the custom module – it won't work if the file is in the vendor folder. If I copy the module files into the app/code folder then it runs perfectly.

Any help would be greatly appreciated.

It is just odd because as soon as I drag the file from the vendor folder which is where composer automatically loads the module into

vendor/[Vendor-Name][Module-Name]/Block/Home/View/Home.php

to the app/code folder – it then works perfectly.

app/code/[Vendor-Name][Module-Name]/Block/Home/View/Home.php

Error:

Class [Vendor-Name][Module-Name]\Block\Home\View\Home does not exist

I also occasionally get this error:

2 exception(s): Exception #0
(Magento\Framework\Exception\LocalizedException): Invalid block type:
Wholesome\Display\Block\Home\View\Home Exception #1
(ReflectionException): Class Wholesome\Display\Block\Home\View\Home
does not exist

Template File: vendor/[Vendor-Name][Module-Name]/view/frontend/templates/home/home.phtml

File: vendor/[Vendor-Name][Module-Name]/Block/Home/View/Home.php

<?php

namespace[Vendor-Name]\[Module-Name]\Block\Home\View;
use \Magento\Framework\View\Element\Template;

class Home extends Template
{

/**
 * Core registry
 *
 * @var \Magento\Framework\Registry
 */
protected $_coreRegistry = null;
/**
 * @var \Magento\Framework\App\Http\Context
 */
protected $httpContext;
protected $pageFactory;
protected $brandPage;

public function __construct(
    \Magento\Framework\View\Element\Template\Context $context,
    \Magento\Framework\Registry $registry,
    \Magento\Cms\Model\PageFactory $pageFactory,
    \Magento\Framework\App\Http\Context $httpContext,
    array $data = []
) {
    $this->_coreRegistry = $registry;
    $this->pageFactory = $pageFactory;
    $this->httpContext = $httpContext;
    parent::__construct($context, $data);
}


}

File: vendor/[Vendor-Name]/[Theme-Name]/layout/cms_index_index.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
   <referenceContainer name="page.top">
     <block class="[Vendor-Name]\[Module-Name]\Block\Home\View\Home" name="wholesome_home" template="home/home.phtml">
        <arguments>
            <argument name="section" xsi:type="string">homepage</argument>
            <argument name="position" xsi:type="number">0</argument>
        </arguments>
     </block>
  </referenceContainer>         
</page>

File: registration.php

\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    '[Vendor-Name]_[Module-Namme]',
    __DIR__
);

Composer.json

{
    "name": "[vendor-name]/[module-name]",
    "description": "",
    "require": {
        "php": "~5.5.0|~5.6.0|~7.0.0|~7.1.12"
    },
    "type": "magento2-module",
    "version": "1.0.1",
    "license": [
        "OSL-3.0",
        "AFL-3.0"
    ],
    "autoload": {
        "files": [
            "registration.php"
        ]
    }
}

Best Answer

Perhaps when you dragged the module, you should remove it from the setup_module and make bin/magento s:d:c and bin/magento setup:upgrade and bin/magento c:c.

and specify the correct namespace and use. I do not see namespace.

I hope that my answer will bring you closer to the solution.

Related Topic