Lesson 10 – Rewrite / Edit Controller Magento

26 septembre 2012

This tutorial is the 10th in this series, if you have not already done it, i suggest you to read the tutorials starting with summary of this series .
There’s a long time that i must write the leçcon 10, this day is finally arrived! The next months i just abandoned the blog, because I do not have much time to post … It’s been a while since I’m talking about a project, i’m still working on and honnestly subscribe to the newsletter because it will provide you an access to my project ! it will be huge !

Finaly there is this famous lesson 10 .. why I have not writen it until now? Because when I did the first blog posts I just did not know how too and I founded it a bit complicated…Since that time now i’ve learned a lot and i’m now magento certified developper Plus…it has been a long road to be there.

ok so now how to make a rewrite of a controller ? Because your module will not khnow where to search for your extended class…you have to make a require in your controller.

   require_once "Mage/Checkout/controllers/CartController.php";

This give you something like:

require_once "Mage/Checkout/controllers/CartController.php";
class Pfay_Checkout_CartController extends Mage_Checkout_CartController
    # Rewrite of indexAction
    public function indexAction() {
        die('your method has been rewrited !!');

the require_once is important if your extends not work (because magento can not autoload that class)

Then your class is taken into account instead of the « classic » CartController of magento, set in the config.xml of your module:

<?xml version="1.0" encoding="UTF-8"?>
             <Pfay_Checkout before="Mage_Checkout">Pfay_Checkout</Pfay_Checkout>

Yes just it and it works! In fact he said « just take the controller Pfay_Checkout in my module before Mage_Checkout like that it loads my class, then this class extends the previous require_once class … and now it works.

Well this article was not so long to do it … in fact I hope it will help someone and it will be useful for you. Sometimes you just have to take the time but after a day of work and training we are tired and we think about thing differently ! All this to say that I would post more often and I thank you for reading this blog :) It is you who motivate me to write and all your messages and comments make me very happy. Thank you again :)

You should read this too :
  • No Related Post
knitzie # 25 octobre, 2012

Hum that did not work…maybe again just without bad chars…

Hi Pierre!
Thanks a lot, this blog brought me the right intension to find my faults in my config.

Maybe you want to add another code snippet to rewrite admin controllers like I wanted to do.
Here’s my snippet:

[Company_Modulename before="Mage_Adminhtml"]Company_Modulename[/Company_Modulename]

This is working especially for overriding Mage_Adminhtml_CustomerController

My Controllerfile (app/code/local/Company/Modulename/controllers/CustomerController.php):

require_once « Mage/Adminhtml/controllers/CustomerController.php »;
class Company_Modulename_CustomerController extends Mage_Adminhtml_CustomerController
public function indexAction()
Mage::log(‘this works!’);

My problem was that I couldn’t figure out what’s the right path for the config.xml and where to save my controller file.
Maybe this helps someone too :)

# 27 septembre, 2013 Victor

Just to clear something up: using before in your XML means that your class will be used INSTEAD of the one normally, and after means that it will be used AFTER the one normally loaded

rocket # 18 décembre, 2013

awesome, I’m a newbie to magento, however your tutorials helped me a lot. Thank Piere

Vous aussi donnez votre avis

Expert magento sur Lille - Pierre FAY