Question

I created an extension for Magento on Magento 2 1.0.0 beta. Now I'm trying to get this extension working for Magento 2 RC, but I can't figure it out. What I have is a very simple extension that creates a block show I can show all my categories in the sidebar (regardless of what category I'm viewing). The extension did work perfect on Magento 1.0.0 beta, but now gives this error: Invalid block type: \SL\Sidebar\Block\Sidebar. The extension is installed correctly. This is the code of my block:

namespace SL\Sidebar\Block;

use Magento\Framework\View\Element\Template;

class Sidebar extends Template {

    /**
     * @var \Magento\Catalog\Helper\Category
     */
    protected $_categoryHelper;

    /**
     * @var \Magento\Framework\Registry
     */
    protected $_coreRegistry;

    /**
     * @var \Magento\Catalog\Model\Indexer\Category\Flat\State
     */
    protected $categoryFlatConfig;

    /**
     * @param Template\Context                                   $context
     * @param \Magento\Catalog\Helper\Category                   $categoryHelper
     * @param \Magento\Framework\Registry                        $registry
     * @param \Magento\Catalog\Model\Indexer\Category\Flat\State $categoryFlatState
     */
    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Catalog\Helper\Category $categoryHelper,
        \Magento\Framework\Registry $registry,
        \Magento\Catalog\Model\Indexer\Category\Flat\State $categoryFlatState,
        $data = []
    ) {
        $this->_categoryHelper = $categoryHelper;
        $this->_coreRegistry = $registry;
        $this->categoryFlatConfig = $categoryFlatState;

        parent::__construct($context, $data);
    }

    /**
     * Get all categories
     *
     * @return array|\Magento\Catalog\Model\Resource\Category\Collection|\Magento\Framework\Data\Tree\Node\Collection
     */
    public function getCategories()
    {
        return $this->_categoryHelper->getStoreCategories();
    }
}

Then in my theme I load the block like this:

<block class="SL\Sidebar\Block\Sidebar" name="category-sidebar" template="Magento_Theme::html/sidebar.phtml" />

Registration.php:

\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'SL_Sidebar',
    __DIR__
);

The extension did work perfect on Magento 1.0.0 beta. Does anyone know the problem here?

Was it helpful?

Solution

I had the same issue with my setup.

When Magento 2 is installed with the composer installer, the autoloader could not find the class.

To solve this, you have to add the following line in the psr-4 section of your root composer.json file.

"SL\\": "app/code/SL/"

Then run composer install and the class will be found

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top