4

How can i allow my controller to work only for logged in users? If non-loggedin user accesses my controller, he should be automatically redirected to login page and after successful login, he reaches my controller. Thanks for answers.

8

Use predispatch function of your controller:

class Some_Module_IndexController extends Mage_Adminhtml_Controller_Action
{

    public function preDispatch()
    {
        parent::preDispatch();
        if (!Mage::getSingleton('customer/session')->authenticate($this)) {
            $this->setFlag('', self::FLAG_NO_DISPATCH, true);
        }
    }

    // other actions

}

And try to replicate functions which you find them another standard Magento modules. For example Mage_Sales_OrderController uses this function which you asked above.

  • Workable, but I think an observer on controller_action_predispatch would be a cleaner way. – Adam Moss Nov 15 '13 at 17:35
  • Why, I have never seen that another Magento standard/community modules used event/observer approach to do this functional. In this case event/observer is little long and difficult way. – mageUz Nov 15 '13 at 17:41

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.