1

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?

2
  • Do you add register.php to module?
    – KAndy
    Commented Nov 11, 2015 at 17:54
  • Yes I did, the module is recognized and enabled.
    – Silvan
    Commented Nov 11, 2015 at 20:47

1 Answer 1

2

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

1
  • 1
    This was the solution to my problem! Awesome, thanks. Instead of composer install (I did not need to fetch the latest packages) I did a composer dump-autoload -o.
    – Silvan
    Commented Nov 13, 2015 at 13:10

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.