SpinOneSolutions | I waste my time so that you don’t have to…

Mar/10

19

Magento Controller Override

The Problem:

You want to override a Controller’s action with a custom action.

Magento Enterprise v1.6.0.0

The Solution:

I ran into some issues trying to override a controller’s action yesterday. I think it mainly stems from a version change, but at any rate here’s the “new” way to do it. This comes from a helpful post on the Magento forums.

In this example my custom modules’s namespace is “Spinonesolutions” and I’m going to override the createPostAction in Mage_Customer_AccountController.

Start by creating a bare bones custom module; if you don’t know how to do this check out Magento – Part I : Custom Module.

I’ve only got two files in my custom module; controllers/AccountController.php and etc/config.xml.

config.xml:

<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
  <Spinonesolutions_Customer>
    <version>0.0.1</version>
  </Spinonesolutions_Customer>
</modules>
<frontend>
  <routers>
    <!-- This is the frontname to override
    EG: http://mydomainname.com/customer/  -->
    <customer>
      <args>
        <modules>
          <!-- Use the module Spinonesolutions_Customer BEFORE Mage_Customer -->
          <Spinonesolutions_Customer before="Mage_Customer">Spinonesolutions_Customer</Spinonesolutions_Customer>
        </modules>
      </args>
    </customer>
  </routers>
</frontend>
</config>

AccountController.php:

<?php
/**
* Override Customer AccountController
*
* Override the createPost action
*
* @category Mage
* @package Mage_Customer
* @author Will Wright
*/

/*Controllers aren't included automatically, so we include it here so that we can extend it*/
include_once("Mage/Customer/controllers/AccountController.php");
class Spinonesolutions_Customer_AccountController extends Mage_Customer_AccountController {
  public function createPostAction() {
    die('here');
  }
}

That’s it!  You should now have a working override.

Share and Enjoy:
  • Print
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • Kirtsy
  • Propeller
  • Reddit
  • Slashdot
  • StumbleUpon
  • Suggest to Techmeme via Twitter
  • Technorati

RSS Feed

8 Comments for Magento Controller Override

Colin | April 15, 2010 at 9:02 am

It should be noted that this method can also be used to add additional controllers within an existing module namespace, not just override existing controllers. An especially useful case is when creating admin controllers and not wanting to pollute the root namespace with your module name.

Will Wright | April 16, 2010 at 7:26 am

Too true! Thanks for the insight!

Eleazar | November 4, 2010 at 3:18 pm

Excelente tutorial, I have been reading many tutorial and could not resolve my problem until now.

I’m using Magento Enterprise V1.8.0
do you have an explanation to override a block?

Thank you

wexy | May 13, 2011 at 4:32 am

Great tutorial. Short, understandable and useful. Thx.

Arvind BHardwaj | June 20, 2011 at 4:00 am

For more information on controller overrdiding in magento see this http://www.webspeaks.in/2011/03/override-controllers-in-magento.html

Rahul | July 26, 2011 at 9:20 am

Great Post please look this URL to show override controller from adminhtml

http://www.justwebdevelopment.com/blog/2011/07/25/override-magento-admin-controller/

Guillermo | January 1, 2012 at 8:41 pm

thank you soo much, i spent hours trying to figure this out

Leave a comment!

«

»

Find it!

Theme Design by devolux.org