سؤال

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>
هل كانت مفيدة؟

المحلول

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.

نصائح أخرى

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;
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top