How to rewrite controller in Magento 2

To rewrite controller, you can do it by using preference. It mean that you need to put a rule in your router config using before attribute.

Open Mageplaza/HelloWorld/etc/di.xml insert the following block of code inside <config> tag rewrite controller in Magento 2

<config xmlns:xsi="" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">

<router id="standard">
    <route id="mageplaza" frontName="hello">
       <module name="Mageplaza_HelloWorld" before="Magento_Customer" />


This will completely change controller/action of module Magento_Customer with your controller code, so you should extend rewrite controller and make a change on the function which you want. Also, the controller and action in your module must have same name with rewrite controller/action. For example, if you want to rewrite controller: Magento\Customer\Controller\Account\Create.php

You have to register a router like above and create a controller:


Content of Create.php file:

namespace Mageplaza\HelloWorld\Controller\Account;

use Magento\Customer\Model\Registration;
use Magento\Customer\Model\Session;
use Magento\Framework\View\Result\PageFactory;
use Magento\Framework\App\Action\Context;

class Create extends \Magento\Customer\Controller\AbstractAccount
    /** @var Registration */
    protected $registration;

     * @var Session
    protected $session;

     * @var PageFactory
    protected $resultPageFactory;

     * @param Context $context
     * @param Session $customerSession
     * @param PageFactory $resultPageFactory
     * @param Registration $registration
    public function __construct(
        Context $context,
        Session $customerSession,
        PageFactory $resultPageFactory,
        Registration $registration
    ) {
        $this->session = $customerSession;
        $this->resultPageFactory = $resultPageFactory;
        $this->registration = $registration;

     * Customer register form page
     * @return \Magento\Framework\Controller\Result\Redirect|\Magento\Framework\View\Result\Page
    public function execute()
        if ($this->session->isLoggedIn() || !$this->registration->isAllowed()) {
            /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
            $resultRedirect = $this->resultRedirectFactory->create();
            return $resultRedirect;

        /** @var \Magento\Framework\View\Result\Page $resultPage */
        $resultPage = $this->resultPageFactory->create();
        return $resultPage;

If you got this error message: Exception printing is disabled by default for security reasons, this topic may help.

Enjoyed the tutorial? Spread it to your friends!


Comments for How to rewrite controller in Magento 2

Please leave comments if you have any questions, feedbacks.

Module Development

You also may like these Magento 2 Extensions

One Step Checkout

10 reviews

Layered Navigation

12 reviews


no review

People also searched for:

  • magento 2 rewrite controller
  • magento 2 override controller
  • overriding controller Magento 2
  • rewrite controller in magento 2
  • how to rewrite controller