Magento重定向到登录页面,而用户没有登陆登陆

时间:2012-01-07 12:22:27

标签: magento magento-1.5

我想在登陆时将用户重定向到登录页面,而用户尚未登录。

例如: -

example.com ->(not logged in)-> redirect to login page.

example.com ->(logged in)-> redirect to Home page.

我该怎么做?

我发现了一些这样的功能

public function preDispatch()
{
    parent::preDispatch();

    if (!Mage::getSingleton('customer/session')->authenticate($this)) {
        $this->setFlag('', 'no-dispatch', true);
    }
}

我如何使用或在哪里使用它。

希望有些人会有这方面的经验。提前致谢

4 个答案:

答案 0 :(得分:18)

这个保存就是我,

if(!$this->helper('customer')->isLoggedIn()){

  Mage::app()->getFrontController()->getResponse()->setRedirect(Mage::getUrl('customer/account'));

}

我在cms页面块中调用它。希望有人帮助。

答案 1 :(得分:2)

$customerId = (int) $this->getRequest()->getParam('id');
$customer   = Mage::getModel('customer/customer')
                  ->load($customerId);

$userSession = Mage::getSingleton('customer/session');
$userSession->setCustomer($customer);
Mage::dispatchEvent('customer_login', array('customer'=>$customer));

$this->getResponse()->setRedirect(Mage::getUrl('customer/account'));

希望这个帮助

答案 2 :(得分:1)

$redirect_url = Mage::getUrl('customer/account/login/');
$current_url = Mage::helper('core/url')->getCurrentUrl();
if((!$this->helper('customer')->isLoggedIn()) && ($current_url != $redirect_url)){
    Mage::app()->getFrontController()->getResponse()->setRedirect($redirect_url);
}

答案 3 :(得分:0)

下面的代码适用于我的 magento 2

    <?php
namespace BA\BasysGiftCertificate\Controller\CustAccount;
 
use Magento\Customer\Model\Session;
use Magento\Framework\Controller\Result\RedirectFactory;
use Magento\Framework\View\Result\PageFactory;

class Index implements \Magento\Framework\App\ActionInterface
{
    /**
     * @var \Magento\Framework\View\Result\PageFactory
     */
    protected $resultPageFactory;
    /**
     * @var \Magento\Framework\Controller\Result\RedirectFactory
     */
    protected $resultRedirect;
    /**
     * @var \Magento\Customer\Model\Session
     */
    protected $customerSession;

    /**
     * @param \Magento\Framework\Controller\Result\RedirectFactory $resultRedirect
     * @param \Magento\Framework\View\Result\PageFactory $resultPageFactory
     * @param \Magento\Customer\Model\Session $customerSession
     */
    public function __construct(
        RedirectFactory $resultRedirect,
        PageFactory $resultPageFactory,
        Session $customerSession
    ) {
        $this->resultRedirect = $resultRedirect;
        $this->resultPageFactory = $resultPageFactory;
        $this->customerSession = $customerSession;
    }
    /**
     * Default customer account page
     *
     * @return void
     */
    public function execute()
    {
        if (!$this->customerSession->isLoggedIn()) {
            /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
            $resultRedirect = $this->resultRedirect->create();
            $resultRedirect->setPath('customer/account/login');
            return $resultRedirect;
        } else {
            return $this->resultPageFactory->create();
        }
    }
}