0

//App/Code/Local/Acme/Datecontrol/etc/config.xml:

<config>
    <modules>
        <Acme_Datecontrol>
            <version>0.1.0</version>
        </Acme_Datecontrol>
    </modules>
    <frontend>
        <routers>
            <datecontrol>
                <use>standard</use>
                <args>
                    <module>Acme_Datecontrol</module>
                    <frontName>datecontrol</frontName>
                </args>
            </datecontrol>
        </routers>
    </frontend>
    <global>
        <events>
            <controller_front_init_before>
                <observers>
                    <Acme_Datecontrol_Model_Observer>
                        <type>singleton</type>
                        <class>Acme_Datecontrol_Model_Observer</class>
                        <method>checkDate</method>
                    </Acme_Datecontrol_Model_Observer>
                </observers>
            </controller_front_init_before> 
        </events>
    </global>
</config>

And the observer,

//App/Code/Local/Acme/Datecontrol/Model/Observer.php:

class Acme_Datecontrol_Model_Observer {

    public function checkDate($observer) {
        Mage::log('Frontend Page loaded', null, 'FirstObserver.log');
    }
}

3 Answers 3

1
<config>
    <modules>
        <Acme_Datecontrol>
            <version>0.1.0</version>
        </Acme_Datecontrol>
    </modules>
    <global>
        <models>
            <datecontrol>
                <class>Acme_Datecontrol_Model</class>
            </datecontrol>
        </models>
    </global>
    <frontend>
        <routers>
            <datecontrol>
                <use>standard</use>
                <args>
                    <module>Acme_Datecontrol</module>
                    <frontName>datecontrol</frontName>
                </args>
            </datecontrol>
        </routers>
        <events>
            <controller_front_init_before> //does not allow type singleton, try using 'controller_action_predispatch' instead
                <observers>
                    <datecontrol_checkdate> //not important
                        <type>model</type>
                        <class>datecontrol/observer</class>
                        <method>checkDate</method>
                    </datecontrol_checkdate>
                </observers>
            </controller_front_init_before> 
        </events>
    </frontend>
</config> 
4
  • Nope, doesn't seem to work. The controller_action_predispatch event is fired on every page reload, right? Also, I tried singleton and model for the 'type' node but to no avail. Any solution?
    – Ashwin
    Oct 24, 2017 at 16:01
  • Hey, this worked! :D Replacing the acme_datecontrol node under <models> helped. Thanks a bunch, mate! :)
    – Ashwin
    Oct 24, 2017 at 16:22
  • 1
    FYI it would be better to place the whole event in global instead of frontend if you are using 'controller_front_init_before' Oct 24, 2017 at 16:30
  • Thank you. Will keep that in mind. I'm using a different event for now, however.
    – Ashwin
    Oct 24, 2017 at 18:16
0

config.xml

<config>
    <modules>
        <Acme_Datecontrol>
            <version>0.1.0</version>
        </Acme_Datecontrol>
    </modules>
    <global>
        <models>
            <acme_datecontrol>
                <class>Acme_Datecontrol_Model</class>
            </acme_datecontrol>
        </models>
    </global>
    <frontend>
        <routers>
            <datecontrol>
                <use>standard</use>
                <args>
                    <module>Acme_Datecontrol</module>
                    <frontName>datecontrol</frontName>
                </args>
            </datecontrol>
        </routers>
        <events>
            <controller_front_init_before> //Im not sure for this event
                <observers>
                    <Acme_Datecontrol_Model_Observer>
                        <class>acme_datecontrol/observer</class>
                        <method>checkDate</method>
                    </Acme_Datecontrol_Model_Observer>
                </observers>
            </controller_front_init_before> 
        </events>
    </frontend>
</config> 
1
  • This doesn't work. :(
    – Ashwin
    Oct 24, 2017 at 16:02
0

On a 1.6.2 version, I had the same problem and I managed to make it work using the events in the global node, instead of frontend :

<config>
    <modules>
        <Acme_Datecontrol>
            <version>0.1.0</version>
        </Acme_Datecontrol>
    </modules>
    <global>
        <models>
            <datecontrol>
                <class>Acme_Datecontrol_Model</class>
            </datecontrol>
        </models>
        <events>
            <controller_front_init_before>
                <observers>
                    <datecontrol_checkdate>
                        <type>model</type>
                        <class>datecontrol/observer</class>
                        <method>checkDate</method>
                    </datecontrol_checkdate>
                </observers>
            </controller_front_init_before> 
        </events>
    </global>
    <frontend>
        <routers>
            <datecontrol>
                <use>standard</use>
                <args>
                    <module>Acme_Datecontrol</module>
                    <frontName>datecontrol</frontName>
                </args>
            </datecontrol>
        </routers>
    </frontend>
</config> 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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