1

I overridden login controller and placed it in my local directory. I have seperate login.phtml as well. It is working fine. But I would like to have core/Mage/ login as well.

Below is my Mage\Customer\Controllers\AccountController.php

<?php

class Mage_Customer_AccountController extends Mage_Core_Controller_Front_Action
{

    /**
     * Customer login form page
     */
    public function loginAction()
    {
        echo 'Mage'; die;
        if ($this->_getSession()->isLoggedIn()) {
            $this->_redirect('*/*/');
            return;
        }
        $this->getResponse()->setHeader('Login-Required', 'true');
        $this->loadLayout();
        $this->_initLayoutMessages('customer/session');
        $this->_initLayoutMessages('catalog/session');
        $this->renderLayout();
    }

Below is my local directory account controller

<?php
require_once 'Mage/Customer/controllers/AccountController.php';
class Ucs_Customer_AccountController extends Mage_Customer_AccountController
{

.
.
.

    /**
     * Customer login form page
     */
    public function popuploginAction()
    {
        if ($this->_getSession()->isLoggedIn()) {
            $this->_redirect('*/*/');
            return;
        }
        $this->getResponse()->setHeader('Login-Required', 'true');
        $this->loadLayout();
        $this->_initLayoutMessages('customer/session');
        $this->_initLayoutMessages('catalog/session');
        $this->renderLayout();
    }

When I access my overridden loginAction it automatically redirects to core login method.

I am trying to access overridden popuplogin as below

http://localhost/magento/index.php/customer/account/popuplogin/

and core login as below

http://localhost/magento/index.php/customer/account/login/

1 Answer 1

1

If you look at the Mage_Customer_AccountController class, you will see it implements the preDispatch function.

That function checks if you are trying to access an action from the controller that doesn't require the user to be logged in. And the allowed actions are the ones in the $openActions array.
If the user is not allowed, Magento runs the following code, which redirects a non-logged in user to the default login action of Magento.

$this->_getSession()->authenticate($this)

Since that method doesn't load the actions from some variable or anything else, there are two ways to achieve what you need:

  1. Create a new route for your module, without rewriting the /customer route. And your controller would extend the Mage_Core_Controller_Front_Action class instead of the Mage_Customer_AccountController class. Example: http://localhost/magento/index.php/ucs/customer/popuplogin
    You can still make your custom form post to customer/account/loginPost.

  2. Re-implement the preDispatch method in your class, and add your own action as an allowed one. Though, since you do not need the preDispatch of Mage_Customer_AccountController, but you still need its parent's preDispatch, replace parent::preDispatch(); with Mage_Core_Controller_Front_Action::preDsipatch().

This is how the function would look like, if you chose the second method:

public function preDispatch()
{
    // a brute-force protection here would be nice

    Mage_Core_Controller_Front_Action::preDispatch();

    if (!$this->getRequest()->isDispatched()) {
        return;
    }

    $action = $this->getRequest()->getActionName();
    $openActions = array(
        'create',
        'login',
        'logoutsuccess',
        'forgotpassword',
        'forgotpasswordpost',
        'resetpassword',
        'resetpasswordpost',
        'confirm',
        'confirmation',
        'popuplogin',
    );
    $pattern = '/^(' . implode('|', $openActions) . ')/i';

    if (!preg_match($pattern, $action)) {
        if (!$this->_getSession()->authenticate($this)) {
            $this->setFlag('', 'no-dispatch', true);
        }
    } else {
        $this->_getSession()->setNoReferer(true);
    }
}
6
  • I just like to add, that my personal preference is the first option.
    – Dan
    Commented Jun 9, 2015 at 14:56
  • I choose second option and when I try with this, popuplogin template render properly, but Core mage login not renders the login page.
    – Anto S
    Commented Jun 10, 2015 at 6:59
  • @version_0.1 Do you still have that die; in the loginAction function? Did you change the login page's layout? If the login page was rendering correctly before implementing the preDispatch function, I see no reason how this might affect it. What gets rendered when you open the login page?
    – Dan
    Commented Jun 10, 2015 at 7:25
  • I remove the die. Now popuplogin page renders perfectly, but when I open login only header and footer appears not the main area.
    – Anto S
    Commented Jun 10, 2015 at 8:24
  • Did you do any modification to the <customer_account_login> update handle in any of your layout xml files?
    – Dan
    Commented Jun 10, 2015 at 8:26

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.