Lesson 10 – Rewrite / Edit Controller Magento

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"?>
<config>
   ...
<frontend>
<routers>
     <checkout>
         <args>
           <modules>
             <Pfay_Checkout before="Mage_Checkout">Pfay_Checkout</Pfay_Checkout>
           </modules>
         </args>
     </checkout>
   </routers>
</frontend>
</config>

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 :)

Posted in magento.


3 Comments

  1. 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

Laisser un commentaire

Votre adresse de messagerie ne sera pas publiée. Les champs obligatoires sont indiqués avec *

Vous pouvez utiliser ces balises et attributs HTML : <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>