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.
1 Answer
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. Nov 15, 2013 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.– mageUzNov 15, 2013 at 17:41