0
votes

These are the two files:

  1. app/code/core/Mage/Module/controllers/SubscriberController.php
  2. app/code/local/MageNS/Module/controllers/SubscriberController.php

I am trying to override file no.(1), but no result, there is next code:

app/code/local/MageNS/Newsletter/etc/config.xml

    <?xml version="1.0"?>
<config>
    <modules>
        <MageNS_Newsletter>
            <version>0.0.1</version>
        </MageNS_Newsletter>
    </modules>
    <global>
        <controllers>
            <Mage_Newsletter>
                <rewrite>
                    <account>MageNS_Newsletter_Controllers_SubscriberController</account>
                </rewrite>
            </Mage_Newsletter>
        </controllers>
    </global>
</config> 

and app/code/local/MageNS/Module/controllers/SubscriberController.php

    require_once("Mage/Newsletter/controllers/SubscriberController.php");
class MageNS_Newsletter_SubscriberController extends Mage_Newsletter_SubscriberController
{  
    public function test{
        echo "HYLlkpkdpwqkdpqkdqpkdqpkdpqkd";die;
    }
}
1

1 Answers

1
votes

Action controllers are instantiated and resolved differently than the other main class types in Magento (blocks, helpers, and models):

  1. Rewrite via config XML can be achieved but via a different mechanism and convention
  2. By intentional design PSR-0-like autoloading does not work for controller classes in Magento.

It is necessary to add a separate module's controllers directory under the overwritten module's frontName:

<frontend><!-- match to original module config -->
    <routers>
        <newsletter><!-- match to original module config-->
            <args>
                <modules>
                    <MageNS before="Mage_Newsletter">MageNS_Module</MageNS>
                    <!--
                        The above points to app/code/[codePool]/MageNS/Module/controllers/
                        If the value were MageNS_Module_Rewrites then controller matching
                        would begin in app/code/[codePool]/MageNS/Module/controllers/Rewrites/
                    -->
                </modules>
            </args>
        </newsletter>
    </routers>
</frontend>

If you look in Mage_Core_Controller_Varien_Router_Standard::collectRoutes() you will see [where this node is being evaluated to add the indicated directory to the list of matching directories ahead of the original module's controllers directory.

Additional note: it's not significant, but the most ideal include syntax for the parent class definition would be as follows:

include Mage::getModuleDir('controllers','Mage_Newsletter').DS.'SubscriberController.php';
class MageNS_Newsletter_SubscriberController extends Mage_Newsletter_SubscriberController
{  
    //Only methods ending in 'Action' can be invoked directly via HTTP request
    // -------------------v
    public function testAction
    {
        $this->getResponse()->setBody("HYLlkpkdpwqkdpqkdqpkdqpkdpqkd");
    }
}