0

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"
        ]
    }
}
1
  • Please share composer.json and registration.php in question. Commented May 27, 2018 at 6:49

4 Answers 4

1

It ended up being the composer.json file which in the autoload section was missing the psr-4 element.

"autoload": {
    "files": [
        "registration.php"
    ],
    "psr-4": {
      "[Vendor-Name]\\[Module-Name]\\": ""
    }
}
0

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.

1
  • Apologies - I accidentally got rid of the namespace when copy and pasting from the IDE to StackExchange - that shouldn't be the issue. I am not sure what you mean by dragged the module. Ideally I don't have to do any dragging as composer update should load the module into the vendor folder. Thanks
    – Jerryl15
    Commented May 26, 2018 at 8:49
0

I followed this post as i had similar problem. Now module is installed in app/code folder, but when i want to run

sudo php bin/magento setup:upgrade

i get error

Autoload error: Module 'VENDOR_MODULENAME' from '/var/www/html/magento2/sites/magento2_test3/app/code/VENDOR/MODULENAME' has been already defined in '/var/www/html/magento2/sites/magento2_test3/vendor/VENDOR/MODULENAME'

any idea how to solve this ? thanks

1
  • I realise you have probably fixed it by now but thought I'd answer anyway just in case someone comes across this and needs the answer. The error pretty much tells you the problem The same module is in /app/code/VENDOR/MODULENAME and /vendor/VENDOR/MODULENAME. Remove one of them (or even just comment out the code in registration.php or rename the file) and try again and it will work. 🙂
    – Craig
    Commented Jan 12, 2022 at 15:29
0

@macho can you confirm whether your compose.json / composer.lock file contains the detials about the module which you installed on app/code folder? If so, can you remove that and try again.

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.