Magento: Controller URL construction and redirection

Constructing the controller url. In a controller or block, you can just use $this->getUrl($url)
[code language=”php”]
Mage::getUrl(‘module/controller/action’);//Example url pattern
Mage::getUrl(‘helloworld/index/index’);//hellow world index page
Mage::getUrl(‘checkout/cart’);//checkout cart index page
[/code]

Redirecting to an url by getting the global response object. This works anywhere in Magento as long as there is response object.
[code language=”php”]
Mage::app()->getResponse()->setRedirct(Mage::getUrl(‘module/controller/action’));
Mage::app()->getResponse()->setRedirct(Mage::getUrl(‘helloworld/index/index’));
Mage::app()->getResponse()->setRedirct(Mage::getUrl(‘checkout/cart’));
[/code]

Redirecting to an url in an observer function by getting a response object from the observer.
[code language=”php”]
$observer->getResponse()->setRedirect(Mage::getUrl(‘module/controller/action’));
$observer->getResponse()->setRedirect(Mage::getUrl(‘helloworld/index/index’));
$observer->getResponse()->setRedirect(Mage::getUrl(‘checkout/cart’));
[/code]

Redirecting to an url in a controller function.
[code language=”php”]
$this->getResponse()->setRedirect($this->getUrl(‘checkout/cart’));
$this->_redirect(‘checkout/cart’);
[/code]

Search within Codexpedia

Custom Search

Search the entire web

Custom Search