Перейти по URL-адресу
У меня есть сайт, использующий Magento 1.7, и на первой странице у меня есть обязательный логин.
Я реализовал обязательный вход в систему через наблюдателя, который отвечает только за доступ к странице входа, пока пользователь не вошел в систему.
Мой код для Наблюдателя приведен ниже:
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;
}
}
}
Проблема в том, что я хочу разрешить пользователю доступ к странице контактов, даже если он/она не вошел в систему. И именно поэтому я включил использование getCurrentURL()
, getPath()
, и добавлено условие внутри if()
.
Но я все еще не могу получить доступ к страницам контактов. Я не уверен в поведении getCurrentUrl()
. Мне нужно получить URL-адрес кнопки, на которую только что нажал пользователь; но, возможно, getCurrentUrl()
просто возвращает URL текущей страницы, как следует из ее названия.
Существует ли какой-либо доступный метод получения URL-адреса нажатой кнопки? Или, может быть, я что-то упускаю?
Любая помощь будет очень признательна.
2 answers
Если вы хотите разрешить доступ к странице "Свяжитесь с нами", просто добавьте contacts_index_index
в массив $allow
и отмените условие для $contactPaths
Если вы хотите добавить любой другой URL-адрес в массив, вы можете получить полное имя действия, вызвав этот код на этой странице
echo Mage::app()->getFrontController()->getAction()->getFullActionName()
Замените свой код следующим:
<?
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;
}
}
}
Вот хороший модуль для этого доберитесь сюда.