PHP Magento Tips

Use controller_action_predispatch in Observer to Dispatch your Own Event

Rate this posting:
{[['']]}

Normally events are dispatched within blocks and models – all of which are called by particular controllers depending where you are on a Magento website. However there is a very obscure event that is fired within a controller – in fact it is fired every single time you change pages on Magento – and we can use an observer to observer it.

Here we will create one page in Magento admin and Creting Event so when user navigate any section in admin one event will fire every time and with observer we will log data in .log file.

Step 1:

In app/etc/modules/Training_Animal.xml add below code:

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


Step 2:

In app/code/local/Training/Animal/etc/config.xml add below code:

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <Training_Animal>
            <version>1.0.1</version>
        </Training_Animal>
    </modules>
    <admin>
      <routers>
        <adminhtml>
          <args>
            <modules>
              <training after="Mage_Adminhtml">Training_Animal_Adminhtml</training>
            </modules>
          </args>
        </adminhtml>
      </routers>
    </admin>
    <adminhtml>
        <events>
            <controller_action_predispatch> <!-- This is the first trigger when init -->
              <observers>
                <training_animal>
                  <type>model</type>
                  <class>Training_Animal_Model_Observer</class>
                  <method>controllerActionPredispatch</method>
                </training_animal>
              </observers>
            </controller_action_predispatch>
        </events>
        <layout>
            <updates>
                  <training>
                    <file>training/animal.xml</file>
                  </training>
            </updates>
        </layout>
        </adminhtml>
</config> 

Step 3:

In app/code/local/Training/Animal/controllers/Adminhtml/AnimalController.php add below code:

<?php
class Training_Animal_Adminhtml_AnimalController extends Mage_Adminhtml_Controller_Action
{
  public function indexAction() {
  }
}

Step 4:

In app/code/local/Training/Animal/Model/Observer.php add below code:

<?php 

class Training_Animal_Model_Observer
{
    public function controllerActionPredispatch(Varien_Event_Observer $observer)
    {
        $user = Mage::getSIngleton('admin/session')->getUser();

        if($user) {
            $name = $user->getUsername();
        } else {
            $name = '  NOT LOGGED IN!  ';
        }

        // Upisi svaki pristup nekog admina nekoj strani u log file
        Mage::log($name . ' ' . Mage::app()->getRequest()->getPathInfo(), Zend_Log::INFO, 'admin.log', true);
    }
}

Step 5:

In app/design/adminhtml/default/default/layout/training/animal.xml add below code:

<?xml version="1.0"?>
<layout>
</layout>


Now first set Add Secret Key to URLs to No from admin under System >> Configuration >> Advanced >> Admin.

And check your admin Page url as:

yoursite.com/admin/animal

You will find a blank Page instead of 404 not found Page.

Now Navigate any section to admin and check your Var >> log Folder. There is one file called admin.log will be created and you can see user log data in it.
Tag : Magento
0 Comments On "Use controller_action_predispatch in Observer to Dispatch your Own Event"

Back To Top