Frage

I have site using Magento 1.7, and have as a first page a login that is mandatory.

I implemented the mandatory login via an observer, that is in charge of only allowing acces to the login page while the user is not logged in.

My code for the Observer is below:

public function checkForLogin(Varien_Event_Observer $observer)
{
  $allow = array('customer_account_login',
   'customer_account_createpost',
    'customer_account_create',
    'customer_account_forgotpassword',
    'customer_account_resetpassword',
    'customer_account_loginpost',
    'customer_account_forgotpasswordpost',
    'customer_account_resetpasswordpost');

  $currentURL = Mage::helper('core/url')->getCurrentUrl();
  $url = Mage::getSingleton('core/url')->parseUrl($currentUrl);
  $path = $url->getPath();
  $contactPaths = array('/mystore/index.php/es/contacts/');

   //Check if any customer is logged in or not, or if he is clicking the contacts page
   if(!Mage::getSingleton('customer/session')->isLoggedIn() && !in_array(strtolower($observer->getControllerAction()->getFullActionName()),$allow) && !in_array($path,$contactPaths)) 
    {
    Mage::app()->getResponse()->setRedirect(Mage::getUrl('customer/account/login'))->sendResponse();
    //echo var_dump($observer->getControllerAction()->getFullActionName());
    exit;
   }
 }
}

The problem is that I want to allow the user to access the contacts page, even if he/she is not logged in. And that's why I included the use of getCurrentURL(), getPath(), and added a condition inside the if().

But I still can't access the contact pages. I am not sure of the behavior of getCurrentUrl(). What I need to get is the URL of the button that the user just clicked on; but maybe getCurrentUrl() just returns the URL of the current page, as its name suggests.

Is there any method available to get the URL of a clicked button? Or maybe I am missing something?

Any help is much appreciated.

War es hilfreich?

Lösung

If you want to allow access to contact us page just add contacts_index_index in $allow array and undo the condition for $contactPaths

if you want to add any other url in the array you can get the Full action name by calling this code on that page

echo Mage::app()->getFrontController()->getAction()->getFullActionName()

Andere Tipps

Replace your code with this :

<?
public function checkForLogin(Varien_Event_Observer $observer)
{
  $allow = array('customer_account_login',
   'customer_account_createpost',
    'customer_account_create',
    'customer_account_forgotpassword',
    'customer_account_resetpassword',
    'customer_account_loginpost',
    'customer_account_forgotpasswordpost',
    'customer_account_resetpasswordpost');

  $currentURL = Mage::helper('core/url')->getCurrentUrl();
  $path = explode('/',$currentURL);
  //Check if any customer is logged in or not, or if he is clicking the contacts page
   if(!Mage::getSingleton('customer/session')->isLoggedIn() && !in_array(strtolower($observer->getControllerAction()->getFullActionName()),$allow) && !in_array('contacts',$path)) 
    {
    Mage::app()->getResponse()->setRedirect(Mage::getUrl('customer/account/login'))->sendResponse();
    //echo var_dump($observer->getControllerAction()->getFullActionName());
    exit;
   }
 }
}

Here is good module for this get here.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit magento.stackexchange
scroll top