1

This is the first observer I'm writing. I'm testing the observer with the controller_action_predispatch that should fire when I load a page as an example. This is the files I created:

/app/etc/modules/EPS_CustomizationURL.xml

<?xml version="1.0"?>
<config>
    <modules>
        <EPS_CustomizationURL>
            <codePool>local</codePool>
            <active>true</active>
        </EPS_CustomizationURL>
    </modules>
</config>

/app/code/local/EPS/CustomizationURL/etc/config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <EPS_CustomizationURL>
            <version>0.0.1</version>
        </EPS_CustomizationURL>
    </modules>
    <global>
        <models>
            <epscustomizationurl>
                <class>EPS_CustomizationURL_Model</class>
            </epscustomizationurl>
        </models>
    </global>
    <adminhtml>
        <events>
            <!-- observe the event -->
            <controller_action_predispatch>
                <observers>
                    <epscustomizationurl>
                        <class>epscustomizationurl/observer</class>
                        <method>epsURLCreator</method>
                    </epscustomizationurl>
                </observers>
            </controller_action_predispatch>
        </events>
    </adminhtml>
</config>

/app/code/local/EPS/CustomizationURL/Model/Observer.php

<?php 
class EPS_CustomizationURL_Model_Observer {
    public function epsURLCreator($observer) {
        $this->_redirectUrl("http://www.google.com");
    } 
}
?>

So, as far as I understand as soon as my page loads or even before it I should be redirected to http://www.google.com. This is the only way I could think of to know if my observer is firing.

What am I missing? Any wrong naming, wrong event or _redirectUrl is not working? I'm on Magento 1.9.

EDIT: All the caches are disabled so the cache should not be the problem:

enter image description here

I also tried just logging something instead of redirect and changing the event to customer_login:

<?xml version="1.0"?>
<config>
    <modules>
        <EPS_CustomizationURL>
            <version>0.0.1</version>
        </EPS_CustomizationURL>
    </modules>
    <global>
        <models>
            <epscustomizationurl>
                <class>EPS_CustomizationURL_Model</class>
            </epscustomizationurl>
        </models>
        <events>
            <!-- observe the event -->
            <customer_login>
                <observers>
                    <eps_customizationurl_model_observer>
                        <type>model</type>
                        <class>epscustomizationurl/observer</class>
                        <method>epsURLCreator</method>
                    </eps_customizationurl_model_observer>
                </observers>
            </customer_login>
        </events>
    </global>
</config>

And my observer:

<?php 
class EPS_CustomizationURL_Model_Observer {
    public function epsURLCreator($observer) {
        Mage::log('I just made an Observer!', null, 'system.log', true);
    } 
}

But still can't see anything in /var/log/system.log

  • 1
    you should get an error because the method _redirectUrl does not exist in your class. Did you clear the cache? – Marius Jun 30 '17 at 19:35
  • How is the method called? I cleared cache in Chrome only, what other cache should I clear? – Andres Jun 30 '17 at 19:50
  • the contents of folder var/cache from your magento instance – Marius Jun 30 '17 at 19:55
  • Ok, I just did it, but I still get no error when refreshing my pages – Andres Jun 30 '17 at 21:11
0
<adminhtml>
    <events>
        <!-- observe the event -->
        <controller_action_predispatch>
            <observers>
                 <mynamespace_mymodule_controller_action_predispatch>
                    <class>customizationurl/observer</class>
                    <method>epsURLCreator</method>
                 <mynamespace_mymodule_controller_action_predispatch>
            </observers>
        </controller_action_predispatch>
    </events>
</adminhtml>

Try the above code in Your xml

  • Will not work, his model alias is epscustomizationurl not customizationurl. – sv3n Jul 1 '17 at 9:27
0

config.xml

<global>
    <models>
        <epscustomizationurl>
            <class>EPS_CustomizationURL_Model</class>
        </epscustomizationurl>
    </models>
    <adminhtml>
        <events>
            <!-- observe the event -->
            <controller_action_predispatch>
                <observers>
                    <epscustomizationurl>
                        <class>epscustomizationurl/observer</class>
                        <method>epsURLCreator</method>
                    </epscustomizationurl>
                </observers>
            </controller_action_predispatch>
        </events>
    </adminhtml>
</global>

Observer.php

class EPS_CustomizationURL_Model_Observer
{
    public function epsURLCreator($observer)
    {
        Mage::app()->getFrontController()->getResponse()->setRedirect('http://www.google.com');
    } 
}

Try its global undefined.

  • Is not working either, I reload any page and I'm not being redirected :/ – Andres Jul 1 '17 at 16:40
0

Try this one:

/app/etc/modules/Eps_Customizationurl.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Eps_Customizationurl>
            <codePool>local</codePool>
            <active>true</active>
        </Eps_Customizationurl>
    </modules>
</config>

/app/code/local/Eps/Customizationurl/etc/config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Eps_Customizationurl>
            <version>0.0.1</version>
        </Eps_Customizationurl>
    </modules>
    <global>
        <models>
            <epscustomizationurl>
                <class>Eps_Customizationurl_Model</class>
            </epscustomizationurl>
        </models>
        <helpers>
            <epscustomizationurl>
                <class>Eps_Customizationurl_Helper</class>
            </epscustomizationurl>
        </helpers>
    </global>
    <frontend>
        <events>
            <controller_action_predispatch>
                <observers>
                    <Eps_Customizationurl_Model_Observer>
                        <type>singleton</type>
                        <class>epscustomizationurl/observer</class>
                        <method>epsUrlCreator</method>
                    </Eps_Customizationurl_Model_Observer>
                </observers>
            </controller_action_predispatch>
        </events>
    </frontend>
</config>

Observer.php

<?php 
/* We will redirect to the checkout/cart for exemple */
class Eps_Customizationurl_Model_Observer extends Varien_Event_Observer {
    public function epsUrlCreator($observer) {
        Mage::getSingleton('core/session')->addError('You have been redirected to the cart !');//if you want to display some message.
        $url = Mage::getUrl('checkout/cart') ? Mage::getUrl('checkout/cart'): Mage::getUrl();
        Mage::app()->getFrontController()->getResponse()->setRedirect($url);
        Mage::app()->getResponse()->sendResponse();
        exit;
    } 
}
?>

/app/code/local/Eps/Customizationurl/Helper/Data.php

<?php

class Eps_Customizationurl_Helper_Data extends Mage_Core_Helper_Abstract{

}
0

I was creating the files and folders through FTP. The permission on the files were right but I forgot to set the right permissions for the folders! As soon as I set the folder permissions to 775 it started working perfectly!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.