1

I've made a new module called "Lpf_ModuleUrlBackAfterLogin" especially to override a method from Mage/Customer/controllers/AccountController.php, using following files .

I want my method "_loginPostRedirect" to be called, instead of the one from "Mage_Customer_AccountController".

When I run my website, the new module seems not to be called (the former "_loginPostRedirect" method is called). I wonder why..

etc/modules/Lpf_ModuleUrlBackAfterLogin.xml

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

etc/modules/Lpf_All.xml

<config>
    <modules>
        <Lpf_Module>
            <active>true</active>
            <codePool>local</codePool>
        </Lpf_Module>
    </modules>
</config>

code/local/Lpf/ModuleUrlBackAfterLogin/etc/config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Lpf_ModuleUrlBackAfterLogin>
            <version>0.1</version>
        </Lpf_ModuleUrlBackAfterLogin>
    </modules>
    <frontend>
       <routers>
            <checkout>
             <args>
                <modules>
                 <Lpf_ModuleUrlBackAfterLogin before="Mage_Customer">Lpf_ModuleUrlBackAfterLogin</Lpf_ModuleUrlBackAfterLogin>
                </modules>
             </args>
            </checkout>
       </routers>
    </frontend>
</config>

code/local/Lpf/ModuleUrlBackAfterLogin/controllers/AccountController.phtml

<?php

require_once "Mage/Customer/controllers/AccountController.php";
class Lpf_ModuleUrlBackAfterLogin_AccountController extends Mage_Customer_AccountController
{
    # surcharge de _loginPostRedirect
    /**
     * Define target URL and redirect customer after logging in
     */
    protected function _loginPostRedirect()
    {
        $session = Mage::getSingleton("core/session");
        $urlBeforeLoginSuccess = $session->getData("urlBeforeLoginSuccess");
        $session = $this->_getSession();

        if (!$session->getBeforeAuthUrl() || $session->getBeforeAuthUrl() == Mage::getBaseUrl() ) {

            // Set default URL to redirect customer to
            $session->setBeforeAuthUrl(Mage::helper('customer')->getAccountUrl());

            // Redirect customer to the last page visited after logging in
            if ($session->isLoggedIn())
            {
                if (!Mage::getStoreConfigFlag('customer/startup/redirect_dashboard')) {
                    if ($referer = $this->getRequest()->getParam(Mage_Customer_Helper_Data::REFERER_QUERY_PARAM_NAME)) {
                        $referer = Mage::helper('core')->urlDecode($referer);
                        if ($this->_isUrlInternal($referer)) {
                            $session->setBeforeAuthUrl($referer);
                        }
                    }
                }
                else if ($session->getAfterAuthUrl()) {
                    $session->setBeforeAuthUrl($session->getAfterAuthUrl(true));
                }
            } else {
                $session->setBeforeAuthUrl(Mage::helper('customer')->getLoginUrl());
            }
        } else if ($session->getBeforeAuthUrl() == Mage::helper('customer')->getLogoutUrl()) {
            $session->setBeforeAuthUrl(Mage::helper('customer')->getDashboardUrl());
        }
        else {
            if (!$session->getAfterAuthUrl()) {
                //Removed $session->setAfterAuthUrl($session->getBeforeAuthUrl());
                $session->setAfterAuthUrl($urlBeforeLoginSuccess);
                Mage::log(__METHOD__ . 'surcharge urlBeforeLoginSuccess '.$urlBeforeLoginSuccess);
            }
            if ($session->isLoggedIn()) {
                $session->setBeforeAuthUrl($session->getAfterAuthUrl(true));
                //Mage::log(__METHOD__ . ' 6 '.$urlBeforeLoginSuccess);
            }
        }
        $this->_redirectUrl($session->getBeforeAuthUrl(true));
    }
}
1

In config.xml of your module replace the following code

<?xml version="1.0"?>
    <frontend>
       <routers>
            <customer>
             <args>
                <modules>
                 <Lpf_ModuleUrlBackAfterLogin before="Mage_Customer">Lpf_ModuleUrlBackAfterLogin</Lpf_ModuleUrlBackAfterLogin>
                </modules>
             </args>
            </customer>
       </routers>
    </frontend>
</config>

require_once(Mage::getModuleDir('controllers','Mage_Customer').DS.'AccountController.php');

Above code for include the AccountController in your controller

  • Thanks for the tips. I don't understand with what content I should replace config.xml. As the one you provided is similar to mine... – jolyqr Mar 15 '15 at 13:26
  • hello jolyqr just replace code from <frontend> tag to </frontend> .can you join me at skype:kinex.sourav – Sourav Mar 15 '15 at 13:31
3

First, code/local/Lpf/ModuleUrlBackAfterLogin/controllers/AccountController.phtml has the wrong file extension. You want php, not phtml.

Second, since this is an older version of Magento, you might want to try the config-based rewrites like such:

<global>
  <routers>
    <cms><!-- route name --> 
      <rewrite> 
        <page><!-- controller name -->
          <actions>
            <!-- rewrite only the view action -->
            <view><!-- action name -->
              <to>target_route/target_controller/target_action</to>
            </view> 
          </actions> 
        </page> 
      </rewrite>
    </cms> 
  </routers> 
</global>

This was the way to do rewrites in legacy magento (pre 1.5.x).

This may or may not get you where you want to go, but it can be a starting point.

  • Thank you Ryan Street ! I've modified my file extension. But I'm pretty knew in magento, so I don't really know how to adapt my old "config.xml" with yours. Could you please provide the new "config.xml" ? – jolyqr Mar 12 '15 at 22:26
2

The <frontend><router> handle in code/local/Lpf/ModuleUrlBackAfterLogin/etc/config.xml is wrong. It should be <customer> instead of <checkout>:

<frontend>
       <routers>
            <customer>
               ...
            </customer>
       </routers>
</frontend>
  • Thanks ! I made the changes, but still not working... – jolyqr Mar 12 '15 at 19:36
1

When overrinding any function from code/local/Lpf/ModuleUrlBackAfterLogin/controllers/AccountController.phtml. The method "indexAction" should always be overriden (otherwise it won't work) as follows if no custom codes need to be run,

public function indexAction()
{
    //no custom codes. 
    parent::indexAction();
}

or as follows whether custom codes need to be run

public function indexAction()
{
 // insert your custom code here...
}

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.