Magento – Extend Magento Controller AND create new action

magento-1.7magento-1.8

I am creating a new module for Facebook Login (no i dont want to use the existing ones) and I am attempting to access/extend the core AccountController.php actions while also adding a new action. The reason I want to extend the AccountController.php class is because I want to register the Facebook user in my custom action and then use some of the functions in the core.

When a user registers, magento uses loginPostAction(). When my Facebook user registers I want to use fbLoginPostAction() in my own AccountController.php in my module and then call some functions in the core AccountController.php.

  • $this->_dispatchRegisterSuccess($customer);
  • $this->_successProcessRegistration($customer);

I've tried several variations but I can't seem to extend the core account controller AND add my custom action. I've been able to extend it and overwrite the existing.

I need to call these functions as I need to dispatch the event after the customer registers.

Am I going about this the right way?

    <?xml version="1.0"?>
    <config>
        <modules>
            <ModEgo_Facebooklogin>
                <version>0.1.0</version>
            </ModEgo_FacebookLogin>
        </modules>
        <frontend>
            <routers>
                <customer>
                    <args>
                        <modules>
                            <modego_facebooklogin before="Mage_Customer">ModEgo_Facebooklogin</modego_facebooklogin>
                        </modules>
                    </args>
                </customer>
            </routers>
        </frontend>          
    </config>    

app/code/local/ModEgo/Facebooklogin/controllers/AccountController.php

    <?php

    require_once Mage::getModuleDir('controllers', 'Mage_Customer').DS.'AccountController.php';
    class ModEgo_FacebookLogin_AccountController extends Mage_Customer_AccountController
    {   
        public function fbLoginPostAction() {
            // this new action wont work 
            echo 'hey';
        }
        public function loginPostaction() {
            // this works
            echo 'hey2';
        }
    }

Best Answer

By "this new action won't work" I think you meant that you are redirected to login, no? Take a look at Mage_Customer_AccountController::preDispatch():

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

    parent::preDispatch();

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

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

    if (!preg_match($pattern, $action)) {
        if (!$this->_getSession()->authenticate($this)) {
            $this->setFlag('', 'no-dispatch', true);
        }
    } else {
        $this->_getSession()->setNoReferer(true);
    }
}

There's a whitelist of actions which can be accessed without logging in ($openActions). You will need to override the preDispatch() method in your controller class as well, adding your fbLoginPostAction() action to the list.

I wish there were a way to specify these externally :-/