I was trying to check few things on a function defined on the block.So if that condition doesn't match, I need to redirect from that function defined on that block to the controller action.

Can anyone please suggest how I achieve this?

Also how can I achieve the same using objectmanager not using factory method.

Can you suggest both ways on doing it.

  • Can you put some example code that you tried?
    – Sohel Rana
    Jan 5, 2017 at 11:26
  • @Qaisar satti How is this duplicate above question is to redirect from controller not the block. Jan 5, 2017 at 11:52
  • @sohel I just want the code that redirects from the block, there is no need of my code here. Those that i have tried didn't work Jan 5, 2017 at 11:52
  • Redirect from block method is very bad idea. Can you describe your task for determining other way to solve your problem?
    – Max
    Jan 5, 2017 at 14:00

4 Answers 4

protected $response;

public function __construct(
   \Magento\Framework\App\Response\Http $response
    $this->response = $response;

 $this->response->setRedirect('url here');


  • its not working!! Aug 4, 2018 at 8:44
  • it is not working. is there any other way to redirect from block? May 8, 2019 at 5:49

Do not use the objectmanager. Using Dependency Injections improves the quality of your module by decreasing coupling and thus increasing extendability. This makes better modules and saves time in the future. The last thing we need is teaching people to use dirty solutions because of inadequacy. If we all make high quality modules for Magento 2 it will improve the platform and everyone profits.

Inject the RedirectFactory in your Block constructor:

private $redirectFactory;

public function __construct(
        \Magento\Framework\Controller\Result\RedirectFactory $redirectFactory,
        \Magento\Framework\View\Element\Template\Context $context,
        $this->productRepository = $productRepository;
        $this->redirectFactory = $redirectFactory

Call the redirect from somewhere in the Block:

    return $this->redirectFactory->create()
  • +1 for "Do not use objectmanager" Mar 7, 2019 at 18:12

In your block constructor, inject these two classes: \Magento\Framework\App\Response\Http and Magento\Framework\App\Response\RedirectInterface

protected $redirect;
protected $response;

public function __construct(
    Context $context,
    \Magento\Framework\App\Response\Http $response,
    \Magento\Framework\App\Response\RedirectInterface $redirect,
    array $data = []
) {
    $this->redirect = $redirect;
    $this->response = $response;
    parent::__construct($context, $data);

Use like this:

$this->redirect->redirect($this->response, 'catalog/product/view/id/1');

You can use like this

filename.php file path should be like this.


$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$this->_backendUrl = $objectManager->get('\Magento\Backend\Model\UrlInterface'); 
$url = $this->_backendUrl->getUrl("<router_name>/actions/<filename>");

For frontend URL redirect you have to use \Magento\Framework\UrlInterface

  • 1
    I was asking for redirecting from block can you post something related to that Jan 5, 2017 at 11:59
  • you can add this code in your block class. Jan 5, 2017 at 12:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.