Redirect functions in magento

Following redirect functions are present in app/code/core/Mage/Core/Controller/Varien/Action.php file.

  1. Redirect specific url : _redirectUrl($url);
  2. Redirect to specific path :  _redirect($path, $arguments=array());
  3. Success page redirection : _redirectSuccess($defaultUrl);
  4. Error page redirection : _redirectError($defaultUrl);
  5. Set referer url to redirect : _redirectReferer($defaultUrl=null);
  6. Get referer url via HTTP_REFERER, regular or requested param : _getRefererUrl()

Examples

In your controller class, you can use as follows

 $this->_redirect($path, $arguments=array());
 // or use,
 $this->_redirectUrl($url);

Here is another way,

//For simple url redirect:

 $url = "http://blog.a2bizz.com";
 Mage::app()->getFrontController()->getResponse()->setRedirect($url);

// Redirect with arguments
 Mage::app()->getFrontController()->getResponse()->setRedirect(Mage::getUrl($path, $arguments));

Magento 2 : Return a json response from a controller

Here the way to return a json response from admin controller

<?php namespace A2bizz\Xyz\Controller\Adminhtml\Abc; 
class  A2bizz extends \Magento\Backend\App\AbstractAction { 
/** 
 * @param \Magento\Backend\App\Action\Context $context 
 * @param \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory 
 */ 
public function __construct( 
    \Magento\Backend\App\Action\Context $context ,
    \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory 
    ) { 
         parent::__construct($context); 
         $this->resultJsonFactory = $resultJsonFactory;
      }

 /**
 * @return \Magento\Framework\Controller\Result\Json
 */
public function execute() {
 /** @var \Magento\Framework\Controller\Result\Json $result */
 $result = $this->resultJsonFactory->create();
 return $result->setData(['success' => true]);
 }
}

etc/adminhtml/routes.xml:

<?xml version='1.0'?>
<config xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:noNamespaceSchemaLocation='../../../../../../lib/internal/Magento/Framework/App/etc/routes.xsd'>
	<router id='admin'>
		<route id='adminhtml' frontName='admin'>
                  <module name='A2bizz_Xyz' before='Magento_Backend' />
		</route>
	</router>
</config>