There are often times where you may want to add your own custom action to the Mage_Customer_AccountController. You may have a module which falls under the category of a “customer action”. You may have already tried and got redirected to the login action. This can be infuriating, so let’s take a closer look at what’s really happening.
In any controller which inherits from Mage_Core_Controller_Front_Action, we have a preDispatch function which is called before we actually get to the specific action which was requested. (If you go even further up the inheritance, you’ll find Mage_Core_Controller_Varien_Action’s preDispatch function which has observer events specific to a controller which are handy to hook into carefully if you need to before an action is run – ‘controller_action_predispatch_’ . $this->getFullActionName(). But this is beside the point from what we’re doing here.) This function is to get everything set properly before we execute code in our specific action.
Let’s open up the preDispatch function in Mage_Customer_AccountController
In this function, we first run our parent’s preDispatch function, which if you traverse to runs important internal Magento / Varien cookie settings and information. You can see that we have a variable called $openActions which actually lists all actions we can hit on the frontend. You then can see it uses a regex pattern to match if the current action is within the open actions. If it doesn’t match any of these actions, you can see it will try to authenticate the current customer session.
How to set up the authenticate function
* Authenticate controller action by login customer
First of all, if we’re already logged in we’ve already been authenticated, so we will return true. We are not passing in a $loginUrl, therefore we will be setting a redirectWithCookieCheck to the constant (url) ROUTE_ACCOUNT_LOGIN – which is the login URL. This just sets a redirect to the account login page. This all looks a little confusing, which is why it may be a little confusing to figure out what’s going on. But fear not! All we need to do is add our custom action when overriding Mage_Customer_AccountController. Please see below:
Notice that we’re calling the parent of Mage_Customer_AccountController’s preDispatch() function which is important instead of just calling the PHP reference ‘parent’ as this would just call Mage_Customer_AccountController’s preDispatch() function. This would in turn override our changes to this function and importantly the $openActions variable where we defined our own actions.