Magento : Surcharger facilement un contrôleur

En cherchant comment surcharger un contrôleur dans Magento, on tombe sur plein de façons différentes de procéder. Certaines fonctionnent, d’autres non. Je vais vous montrer comment surcharger un contrôleur très facilement.

Cas d’un contrôleur de l’admin

Modification du fichier config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Namespace_Adminhtml>
            <version>0.1.0</version>
        </Namespace_Adminhtml>
    </modules>
    <admin>
        <routers>
            <adminhtml>
                <args>
                    <modules>
                        <Namespace_Adminhtml before="Mage_Adminhtml">Namespace_Adminhtml</Namespace_Adminhtml>
                    </modules>
                </args>
            </adminhtml>
        </routers>
    </admin>
</config>

Surcharge du contrôleur

Créer le fichier :

app/code/local/Namespace/Adminhtml/controllers/Cms/PageController.php
<?php
 
require_once 'Mage/Adminhtml/controllers/Cms/PageController.php';
 
class Namespace_Adminhtml_Cms_PageController extends Mage_Adminhtml_Cms_PageController
{
    public function indexAction()
    {
        // My stuff
    }
}

Cas d’un contrôleur du front

Modification du fichier config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Namespace_Cms>
            <version>0.1.0</version>
        </Namespace_Cms>
    </modules>
    <frontend>
        <routers>
            <cms>
                <args>
                    <modules>
                        <Namespace_Cms before="Mage_Cms">Namespace_Cms</Namespace_Cms>
                    </modules>
                </args>
            </cms>
        </routers>
    </frontend>
</config>

Surcharge du contrôleur

Créer le fichier :

app/code/local/Namespace/Cms/controllers/IndexController.php
<?php
 
require_once 'Mage/Cms/controllers/IndexController.php';
 
class Namespace_Cms_IndexController extends Mage_Cms_IndexController
{
    public function indexAction()
    {
        // My stuff
    }
}

Et voila :)

10 réponses à “Magento : Surcharger facilement un contrôleur”

  1. Raja dit :

    hye, dear Johann it is not working for me, i am using magento 1.6.2.0, and nothing happens same core controller is called. can u please help?

  2. manik dit :

    Hey Johann,
    I’ve created a custom module manik_ajax, it shows active in the modules list but I’m not able to override CartController.php, it is weird because it works perfectly in my local machine (Mac OSX) but doesn’t in my Server (Linux, Ubuntu), I’ve found out that mostly its because of case issues but cases seem alright to me, please help me:
    app/code/local/manik/ajax/controllers/Checkout/CartController.php – It is the copy of original file except an echo ‘hello world'; command in index action
    app/code/local/manik/ajax/etc/config.xml

    0.0.1

    manik_ajax_Checkout

  3. manik dit :

    Since the xml was omitted here’s the xml code http://tinypaste.com/2c0aa0df

  4. Robin dit :

    Hi,

    thanks for the article.Let me clear one doubt if my custom module have the same name of the module which am going to override what will happen?

  5. Roman dit :

    Thank you. You saved a lot of time for me.

  6. Ravi dit :

    I have to override local module controller method by local module. how can i?

  7. Andrey dit :

    This is completly useless!!!
    For a person that is trying to learn magento this is so confusing.
    For example:
    First config.xml use this namespace «  » then in second the namespace change to this one «  ».
    No explanations about this happens.
    No folders where to put this files.

    I come here looking for a solution, but now Im more confuse.

  8. Victor dit :

    Works like a charm. Thanks!

Laisser un commentaire

* Champs requis

Categories