0

My goal is to save on database the url referer and keywords when entering on any page of our store. I checked this question and its answer to try to get some light into my problem.

The approach I'm following is:

  1. To have a block that will stick on the default layout so it will be loaded on each page (tested, is working). This block will read the referer by using the following code:

    $request['url'] = $this->getRequest()->getServer('HTTP_REFERER');

  2. Then call function passing the $request to a controller: This step I still do not know how to do it, using redirect? Or maybe by calling a dispatchEvent and on the controller having a postAction function?

    //on the phtml file
    <div>
         <?php
              $request['url'] = getRequest()->getServer(‘HTTP_REFERER’);
              Mage::dispatchEvent("allpagescontroller", $request);
         ?>
    </div>
    
  3. The controller will instantiate my model and call the setters to update/insert neeeded values

    //on the controller
    function postAction ($params)
    {
        $referer = $this->getRequest()->getPost();
    }
    
  4. On the model I will have all the appropiate code to do the CRUD operations

Is this correct? I'm trying to follow the Magento MVC approach by following these steps. For now I've all the code on the phtml file. I'm getting the referer, splitting it and instantiating the model to save everything. But I know this is not correct.

On config.xml for now I have:

<?xml version="1.0"?>
<config>
    <modules>
        <Dts_Allpages>
            <version>0.1.0</version>
        </Dts_Allpages>
    </modules>
    <global>
      <models>
          <allpages>
              <class>Dts_Allpages_Model</class>
              <resourceModel>allpages_mysql4</resourceModel>
          </allpages>
          <allpages_mysql4>
                  <class>Dts_Allpages_Model_mysql4</class>
                <entities>
                  <keywords>
                      <table>keywords</table>
                  </keywords>
                  <referencedpages>
                      <table>referencedpages</table>
                  </referencedpages>
              </entities>
          </allpages_mysql4>
        </models>
        <blocks>
            <allpages>
                <class>Dts_Allpages_Block</class>
            </allpages>
        </blocks>
        <helpers>
            <allpages>
                <class>Dts_Allpages_Helper</class>
            </allpages>
        </helpers>
        <resources>
            <allpages_write>
                <connection>
                    <use>core_write</use>
                </connection>
            </allpages_write>
            <allpages_read>
                <connection>
                    <use>core_read</use>
                </connection>
            </allpages_read>
        </resources>
    </global>>
    <frontend>
      <routers>
          <allpages>
              <use>standard</use>
              <args>
                  <module>Dts_Allpages</module>
                  <frontName>allpages</frontName>
              </args>
          </allpages>
      </routers>
        <layout>
            <updates>
                <allpages>
                    <file>allpages.xml</file>
                </allpages>
            </updates>
        </layout>
    </frontend>  
</config>
Community
  • 1
  • 1
Yaroslav
  • 6,476
  • 10
  • 48
  • 89

2 Answers2

0

Your logic is not correct in terms of Magento MVC flow. Template is the last stop of the program flow, you should not insert ANY logic in templates, because your system will transform into unstable mess.

The logic you're trying to achieve does not require any view at all. It doesn't require even Controller. As you know, Magento utilizes an Event-Observer pattern, and your task is just asking for it.

You should create your Observer and listen to some event from the initial request flow. You can listen to any event that is triggered on every page request, but it shouldn't be too early - when system itself is still not yet initialized, and it shouldn't be too late, when some of the other system logic can redirect, forward or make some changes to referrer. I think controller_action_predispatch will suit your needs.

In the Observer you can get the HTTP_REFERER and save it in the DB using your Model layer.

Slayer Birden
  • 3,664
  • 2
  • 22
  • 29
  • Again the EventObservers on my way...I was doubting if that was the correct path to follow. Not you cleared it for me. So I do not need to add blocks, templates or controller, just all the logic for the event observer. Will test it. – Yaroslav Sep 26 '12 at 11:41
  • I'm stuck on how to listen to all pages predispatch event. – Yaroslav Sep 26 '12 at 13:24
  • @Yaroslav - here's an example of config you should insert to your custom module `allpages/observersaveReferrer` You should create an Observer model with saveReferrer method, and it should be triggered on every page request – Slayer Birden Sep 26 '12 at 14:04
  • Thanks. I was close to this, as a couple of weeks ago I was struggling with event/observer pattern. That is way in my comment I said "Again the EventObserver on my way...". Will test and comeback to you. – Yaroslav Sep 26 '12 at 14:12
  • I was having an error, managed to solve it by replacing etiquette `` by ``. Is that ok? If not I was receiving a file not included when searching for the Observer.php on Dts\AllPages\Model\Observer.php – Yaroslav Sep 26 '12 at 14:20
  • The exact error was `Warning: include(Dts\Allpages\Model\Obs‌​erver.php) [function.include]: failed to open stream: No such file or directory in C:\wamp\www\magento\lib\Varien\Autoload.php on line 93` and some more code – Yaroslav Sep 26 '12 at 14:27
  • `` here is just an alias of your Observer process. Yeah and when I said you should create your Observer model, I meant exactly - to create Dts\AllPages\Model\Observer.php file with class Dts_AllPages_Model_Observer. and in this class - method `saveReferrer` – Slayer Birden Sep 26 '12 at 14:27
  • Understood, and that is what I have. Now testing but, weird, no results. I just added a `Mage::Log('Now in');` on the saveReferrer method and I don't get nothing. Is like if the observer is not recognized. I'm using CommerceBug from @AlanStorm and on the Class/URI Lookup tab I enter allpages/observer and it's returning the correct path and class name...I'm getting ansious... need more coffee – Yaroslav Sep 26 '12 at 14:43
  • The event I've proposed is dispatched in `Mage_Core_Controller_Varien_Action::preDispatch` method. Though if you're developing for EE with PageCache turned on, it's possible you get your page from cache omitting the normal request flow... so you'll have to find the event that suit your needs.. What you can do is next - just add logging to `Mage::dispatchEvent` method - smth like `Mage::log($name, null 'your.log')` - so you'll know what event s and in what order are dispatched on your page – Slayer Birden Sep 26 '12 at 14:53
  • hmmm...My cache is all disabled, it was one of the first things recommended everywhere when on testing enviroment. I also have Inchoo plugin to swith between Developer/Production mode easily. I want to capture __ALL__ pages so I can save all referrers and keywords – Yaroslav Sep 26 '12 at 15:01
  • ARGGHHH!!!!!!!!! sorry for this but magento is driving me crazy... I thought that I could have as many event observers as I liked. But seems that if there are two equal events it captures the first was created. And indeed I have another module that captures too the preDispatch event. Went to the module etc file and changed that module `false`. Now it's working. I'll continue on the saving referrers part. thanks again. – Yaroslav Sep 26 '12 at 16:08
0

Use this code in observer

$url = Mage::helper('core/http')->getHttpReferer() ? Mage::helper('core/http')->getHttpReferer()  : Mage::getUrl();
Mage::app()->getFrontController()->getResponse()->setRedirect($url);
Mage::app()->getResponse()->sendResponse();
exit;
Ashwani Panwar
  • 3,819
  • 3
  • 46
  • 66