Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Page 1 of 2
How to redirect to a different module/controller/action? [SOLVED]
 
Vinai
Sr. Member
 
Avatar
Total Posts:  218
Joined:  2008-04-04
Heidelberg, Germany
 

Hi,

my code in an event observer:

$request Mage::app()->getRequest();
 
$request->setModuleName('customer')
  ->
setControllerName('account')
  ->
setActionName('login')
  ->
setDispatched(false);

the Result:

WarningSimpleXMLElement::addAttribute() [simplexmlelement.addattribute]Attribute already exists  in /var/www/htdocs/app/code/core/Mage/Core/Model/Layout.php on line 159

How do I redirect the right way to a different module?

My current hack to work around the problem:

$response Mage::app()->getResponse()
  ->
setHeader("Location""/customer/account/login")
  ->
sendHeaders();
Works but it’s ugly and breaks if the base url isn’t /
So btw - anybody got a pointer how to get the correct base url?

Thanks,

Vinai

 
Magento Community Magento Community
Magento Community
Magento Community
 
Vinai
Sr. Member
 
Avatar
Total Posts:  218
Joined:  2008-04-04
Heidelberg, Germany
 

*bump*
How do I redirect to a different module / controller ?
I really want to do it the magento way…

Thanks,
Vinai

 
Magento Community Magento Community
Magento Community
Magento Community
 
bcelenza
Member
 
Avatar
Total Posts:  44
Joined:  2008-08-27
 

The only way I’ve found that works to my liking is:

$this->getResponse()->setRedirect("http://www.disneyworld.com"); (taken from my controller code)

Sorry if this doesn’t produce the desired result for you.

 
Magento Community Magento Community
Magento Community
Magento Community
 
LeeSaferite
Guru
 
Avatar
Total Posts:  322
Joined:  2007-08-31
Lake City, FL
 

Does this help any?

Mage::app()->getResponse()->setRedirect(Mage::helper('adminhtml')->getUrl("customer/account/login", array('extra'=>'params''go'=>'here')));

NOTE: Fixed bug to prevent later cut/paste issues for other readers

 
Magento Community Magento Community
Magento Community
Magento Community
 
LeeSaferite
Guru
 
Avatar
Total Posts:  322
Joined:  2007-08-31
Lake City, FL
 

Whoa, that should be

Mage::app()->getResponse()->setRedirect(Mage::helper('adminhtml')->getUrl("customer/account/login", array('extra'=>'params''go'=>'here')));

And you can use ‘*’ instead of your module, controller, and/or action to specify that it should use the current one.

 
Magento Community Magento Community
Magento Community
Magento Community
 
LeeSaferite
Guru
 
Avatar
Total Posts:  322
Joined:  2007-08-31
Lake City, FL
 

I think you can get the BaseUrl you want by using Mage::getBaseUrl();

 
Magento Community Magento Community
Magento Community
Magento Community
 
Vinai
Sr. Member
 
Avatar
Total Posts:  218
Joined:  2008-04-04
Heidelberg, Germany
 

Thanks for the answers.

@Lee Saferite: which object has the url property?
I’m using the code in an observer, so $this doesn’t work.
Mage_Core_Controller_Response_Http hasn’t got that either…
Thanks in advanced!

EDIT: OH, just noticed you corrected yourself!
Vinai

 
Magento Community Magento Community
Magento Community
Magento Community
 
Vinai
Sr. Member
 
Avatar
Total Posts:  218
Joined:  2008-04-04
Heidelberg, Germany
 

Thats it, thank you very much!

Vinai

 
Magento Community Magento Community
Magento Community
Magento Community
 
harikaram
Member
 
Avatar
Total Posts:  54
Joined:  2008-07-27
 

Hi,

I tried using
Mage::app()->getResponse()->setRedirect(Mage::helper(’adminhtml’)->getUrl("customer/account/login", array(’extra’=>’params’, ‘go’=>’here’)));

And nothing happens.  I tried adding an exit() and again nothing happens (other than the exiting of course smile)

I’m using the event “checkout_cart_add_product_complete” whose calling function (addAction() somewhere...) sets the responses redirect so perhaps its being overwritten?

Is there not a way to get the current controller from withint the Observer so we can use the nice and simple _redirect() method?

Barring that is there not a higher level way to immediately redirect?

 
Magento Community Magento Community
Magento Community
Magento Community
 
harikaram
Member
 
Avatar
Total Posts:  54
Joined:  2008-07-27
 

Some more details…

Here’s the event setup XML

<events>
            <
checkout_cart_add_product_complete>
                <
observers>
                    <
worldpay_observer>
                        <
type>singleton</type>
                        <class>
worldpay/observer/</class>
                        <
method>checkForFuturePay</method>
                    </
worldpay_observer>
                </
observers>
            </
checkout_cart_add_product_complete>     
        </
events>

And the event handler method

public function checkForFuturePay($observer)
    
{
        $event 
$observer->getEvent();
        
$product $event->getProduct();  
        
$cart Mage::getSingleton('checkout/cart');

        
// If this is a FuturePay product then empty the cart of all others and redirect to checkout
        
if ( (int)$product->getFuturepay() ) {

            $cart
->updateItems( array( $product->getId() => array('qty' => 1) ) );
            
$cart->save();
            
header'Location: ' Mage::helper('checkout/url')->getCheckoutUrl() ); 
            exit();
        
}

        
return $this;
    
}

Before I had the line

Mage::app()->getResponse()->setRedirect(...)
where the code
header'Location: ' Mage::helper('checkout/url')->getCheckoutUrl() );
...is now.

I think that ->setResponse()’s actions may be overwritten by the _goBack() method in Mage_Checkout_CartController which is called after the event is dispatched in the method addAction().  Here’s _goBack():

protected function _goBack()
    
{
        
if (!Mage::getStoreConfig('checkout/cart/redirect_to_cart')
            && !
$this->getRequest()->getParam('in_cart')
            && 
$backUrl $this->_getRefererUrl()) {

            $this
->getResponse()->setRedirect($backUrl);
        
else {
            
if (($this->getRequest()->getActionName() == 'add') && !$this->getRequest()->getParam('in_cart')) {
                $this
->_getSession()->setContinueShoppingUrl($this->_getRefererUrl());
            
}
            $this
->_redirect('checkout/cart');
        
}
        
return $this;
    
}

Its the later half of the IF statement that is executing in my case…

This is version 1.6 btw.

Thanks for any help… header(...) works fine but it seems like there should be a way to get access to the controller and use is _redirect method…

 
Magento Community Magento Community
Magento Community
Magento Community
 
strangeelement
Jr. Member
 
Total Posts:  3
Joined:  2009-08-05
 

Doesn’t work for login observer.

Redirect keeps being overwritten by post login redirect.

 
Magento Community Magento Community
Magento Community
Magento Community
 
miked2004
Sr. Member
 
Avatar
Total Posts:  214
Joined:  2007-12-13
Atlanta, Georgia
 

For the redirect to work for me I had to add the below code just after setting the redirect.

Mage::app()->getResponse()->sendResponse();
 
Magento Community Magento Community
Magento Community
Magento Community
 
rocksolidwebdev
Jr. Member
 
Avatar
Total Posts:  3
Joined:  2008-06-26
New York City
 

Actually the adminhtml helper is not strictly necessary, depending on what you’re doing, you may prefer to use the URL model instead. For example, to prevent non-customers from even accessing the checkout page, you could do something like this…

<?php
class Mycompany_Mymodule_Model_Observer
{
    
public function restrictCheckoutToCustomersOnly($observer)
    
{
        
if (!Mage::helper('customer')->isLoggedIn()) {
            Mage
::getSingleton('customer/session')
                ->
addError('Silly rabbit, checkouts are for customers only!');

            
$url Mage::getModel('core/url')
                       ->
getUrl("checkout/cart/index");

            
Mage::app()
                ->
getResponse()
                ->
setRedirect($url);

            
Mage::app()
                ->
getResponse()
                ->
sendResponse();

            exit;
        
}
    }
}

also note that the exit call is important, so don’t forget to put it in there, otherwise your notices and messages won’t show up.

 
Magento Community Magento Community
Magento Community
Magento Community
 
davidselo
Jr. Member
 
Total Posts:  16
Joined:  2010-10-27
 

Thanks, this is very usefult for me.

 
Magento Community Magento Community
Magento Community
Magento Community
 
severinolorilla
Jr. Member
 
Total Posts:  4
Joined:  2012-11-19
 
rocksolidwebdev - 27 May 2011 09:40 AM

<?php
class Mycompany_Mymodule_Model_Observer
{
    
public function restrictCheckoutToCustomersOnly($observer)
    
{
        
if (!Mage::helper('customer')->isLoggedIn()) {
            Mage
::getSingleton('customer/session')
                ->
addError('Silly rabbit, checkouts are for customers only!');

            
$url Mage::getModel('core/url')
                       ->
getUrl("checkout/cart/index");

            
Mage::app()
                ->
getResponse()
                ->
setRedirect($url);

            
Mage::app()
                ->
getResponse()
                ->
sendResponse();

            exit;
        
}
    }
}

Thanks. Works perfectly. smile

 
Magento Community Magento Community
Magento Community
Magento Community
 
Afroz Alam
Jr. Member
 
Total Posts:  6
Joined:  2011-02-10
 

You can use the redirect functions in your controller class. Like below:-

$this->_redirect($path, $arguments=array());
OR,
$this->_redirectUrl($url);

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top
Page 1 of 2