Magento - Redirect back (similar to using setBefor

2019-09-02 14:53发布

问题:

I have the following controller action, which redirects to the login page if no user is logged in:

public function requireloginAction() {
  if(!Mage::getSingleton('customer/session')->isLoggedIn()) {  
    // Not logged in
    // Save requested URL for later redirection
    Mage::getSingleton('customer/session')->setBeforeAuthUrl($this->getRequest()->getRequestUri());
    header("Status: 301");
    header('Location: '.Mage::helper('customer')->getLoginUrl());  // send to the login page
  }
  else {
    // Logged in
    .. do something ..
  }
}

By using setBeforeAuthUrl, once the user logs in he/she is redirected back to this action.

Problem:

If instead of logging in, the user, creates an account he/she is then redirected to the main page, rather then to the url set in setBeforeAuthUrl.

Question:

Is there something similar to setBeforeAuthUrl that works with Account Creation too? Or how can I achieve the desired effect?

(Magento Version 1.6)

回答1:

You can try using the following extension. http://www.magentocommerce.com/magento-connect/MagePsycho/extension/3763/custom_login_redirect

Or you can also open app/code/core/Mage/Customer/controllers/AccountController.php and look for the createPostAction() function around line 328 edit:

     $url = $this->_welcomeCustomer($customer);
     $this->_redirectSuccess($url);

to

     $url = 'http://www.mycustomrediurecturl.com';
     $this->_redirectSuccess($url);

If you want to do it the nice way override the controller add configuration options and make it a module :)

Cheers



回答2:

Found solution.

First of all, setBeforeAuthUrl($url) does work for both "Log In" and "New Account Creation"!

The main difference (and the reason I had the problem) is that for a "New Account Creation" Magento checks if $url is within the domain name of the current store and if it is not, it redirects to the "My Account" page. While the redirection for "Log In" redirects to any $url.

I do not know if this is a bug or a feature (I'm using V1.6.0.0).

So just make sure to redirect to a url within the domain name of the current store - especially in a Multi Store configuration.



标签: magento