Magento 2 – Fix Block Not Found Error

blocksformsmagento2

I have a custom form with block that I want to load, but everytime I call the controller to load the form I always got this error=

Fatal error: Uncaught Error: Class
'Lime\Courier\Block\Adminhtml\Preferences\Edit' not found in
C:\xampp7\htdocs\magento2\vendor\magento\framework\ObjectManager\Factory\AbstractFactory.php:93

I already make sure the block folder structure is right and first folder and file name start with capital letter, I also already delete var/generation folder and run setup:di:compile, but nothing works

here's my xml layout code :

<?xml version="1.0" ?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <update handle="editor"/>
    <body>
        <referenceContainer name="content">
            <block class="Lime\Courier\Block\Adminhtml\Preferences\Edit" name="lime_courier_preferences_edit"/>
        </referenceContainer>
    </body>
</page>

in Lime\Courier\Block\Adminhtml\Preferences\Edit directory I have Edit.php :

<?php
namespace Lime\Courier\Block\Adminhtml\Preferences\Edit;

class Edit extends \Magento\Backend\Block\Widget\Form\Container
{
    /**
     * Core registry
     *
     * @var \Magento\Framework\Registry
     */
    protected $_coreRegistry = null;

    /**
     * @param \Magento\Backend\Block\Widget\Context $context
     * @param \Magento\Framework\Registry $registry
     * @param array $data
     */
    public function __construct(
        \Magento\Backend\Block\Widget\Context $context,
        \Magento\Framework\Registry $registry,
        array $data = []
    ) {
      die('a');
        $this->_coreRegistry = $registry;
        parent::__construct($context, $data);
    }

    /**
     * Initialize blog post edit block
     *
     * @return void
     */
    protected function _construct()
    {
        $this->_objectId = 'preferences_id';
        $this->_blockGroup = 'Lime_Courier';
        $this->_controller = 'lime_courier';

        parent::_construct();

        if ($this->_isAllowedAction('Lime_Courier::save')) {
            $this->buttonList->update('save', 'label', __('Save Courier Preferences'));
            $this->buttonList->add(
                'saveandcontinue',
                [
                    'label' => __('Save and Continue Edit'),
                    'class' => 'save',
                    'data_attribute' => [
                        'mage-init' => [
                            'button' => ['event' => 'saveAndContinueEdit', 'target' => '#edit_form'],
                        ],
                    ]
                ],
                -100
            );
        } else {
            $this->buttonList->remove('save');
        }

        if ($this->_isAllowedAction('Lime_Courier::preferences_delete')) {
            $this->buttonList->update('delete', 'label', __('Delete Preferences'));
        } else {
            $this->buttonList->remove('delete');
        }
    }

    /**
     * Retrieve text for header element depending on loaded post
     *
     * @return \Magento\Framework\Phrase
     */
    public function getHeaderText()
    {
        if ($this->_coreRegistry->registry('lime_courier')->getId()) {
            return __("Edit Preferences '%1'", $this->escapeHtml($this->_coreRegistry->registry('lime_courier')->getTitle()));
        } else {
            return __('Lime Courier');
        }
    }

    /**
     * Check permission for passed action
     *
     * @param string $resourceId
     * @return bool
     */
    protected function _isAllowedAction($resourceId)
    {
        return $this->_authorization->isAllowed($resourceId);
    }

    /**
     * Getter of url for "Save and Continue" button
     * tab_id will be replaced by desired by JS later
     *
     * @return string
     */
    protected function _getSaveAndContinueUrl()
    {
        return $this->getUrl('courier/*/save', ['_current' => true, 'back' => 'edit', 'active_tab' => '']);
    }
}

Best Answer

change your xml file

<?xml version="1.0" ?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <update handle="editor"/>
    <body>
        <referenceContainer name="content">
            <block class="Lime\Courier\Block\Adminhtml\Preferences\Edit" name="lime_courier_preferences_edit"/>
        </referenceContainer>
    </body>
</page>

to

<?xml version="1.0" ?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <update handle="editor"/>
    <body>
        <referenceContainer name="content">
            <block class="Lime\Courier\Block\Adminhtml\Preferences\Edit\Edit" name="lime_courier_preferences_edit"/>
        </referenceContainer>
    </body>
</page>

You missed a Class name from xml file.

Related Topic