Magento – Magento 2.2.5: Overriding Admin Controller sales/order

admin-controllermagento2overrides

I need to override Admin sales/order/index controller, but still getting 404. Below the Steps I take to override –

Created My Custom Module
app/code/A2bizz/Order/registration.php

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'A2bizz_Order',
    __DIR__
);

app/code/A2bizz/Order/etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="A2bizz_Order" setup_version="2.0.0" schema_version="2.0.0">
        <sequence>
            <module name="Magento_Sales"/>
            <module name="Magento_Rule"/>
            <module name="Magento_Catalog"/>
            <module name="Magento_Customer"/>
            <module name="Magento_Payment"/>
            <module name="Magento_SalesSequence"/>
        </sequence>
    </module>
</config>

app/code/A2bizz/Order/Controller/Adminhtml/Order/Index.php

<?php

    namespace A2bizz\Order\Controller\Adminhtml\Order;

    class Index extends \Magento\Sales\Controller\Adminhtml\Order\Index
    {
        public function execute()
        {
            $this->messageManager->addSuccess('Message from new admin controller.');
            // Do your stuff here
            return parent::execute();
        }
    }

app/code/A2bizz/Order/etc/adminhtml/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\Sales\Controller\Adminhtml\Order\Index" 
                type="A2bizz\Order\Controller\Adminhtml\Order\Index" />
</config>

if anyone can help me on what I am doing wrong.
Thanks

Best Answer

It is recommendable use the plugin instead of preference for this.

https://devdocs.magento.com/guides/v2.2/extension-dev-guide/plugins.html

You must do the next.

Vendor/Namespace/etc/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Sales\Controller\Adminhtml\Order\Index">
    <plugin name="vendor_namespace_plugin_list" type="Vendor\Namespace\Plugin\ControllerOrderIndexPlugin"/>
</type>

Vendor/Namespace/Plugin/ControllerOrderIndexPlugin.php

<?php

namespace Vendor\Namespace\Plugin;

use Magento\Framework\Message\ManagerInterface;
use Magento\Sales\Controller\Adminhtml\Order\Index;

/**
 * Class ControllerOrderIndexPlugin
 */
class ControllerOrderIndexPlugin
{

    /**
     * @var ManagerInterface
     */
    private $messageManager;

    /**
     * ControllerOrderIndexPlugin constructor.
     *
     * @param ManagerInterface $messageManager
     */
    public function __construct(ManagerInterface $messageManager)
    {
        $this->messageManager = $messageManager;
    }

    /**
     * @param Index $subject
     */
    public function beforeExecute(
        Index $subject
    ) {
        $this->messageManager->addSuccessMessage(__('Message from new admin controller.'));
    }
}