0

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.

4
  • 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

5 Answers 5

6
protected $response;

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

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

reference

2
  • 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
3

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
        parent::__construct($context);
    }

Call the redirect from somewhere in the Block:

    return $this->redirectFactory->create()
        ->setPath('module/group/controller');
0
2

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');
0

You can use like this

filename.php file path should be like this.

vendor/module/Controller/Adminhtml/Actions/Filename.php

$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

2
  • 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
0

This would be the block class:

use Magento\Framework\View\Element\Template;
use Magento\Framework\App\Response\Http;

class MyCustomBlock extends Template
{
    /**
     * @var Http
     */
    private Http $redirect;
    /**
     * @param Template\Context $context
     * @param CustomerSession $customerSession
     * @param Http $redirect
     * @param array $data
     */
    public function __construct(
        Template\Context $context,
        Http $redirect,
        array $data = []
    ) {
        $this->redirect = $redirect;
        parent::__construct($context, $data);
    }
    public function customRedirect():Http
    {
        return $this->redirect->setRedirect('/my/fancy/url');
    }
}

From the template:

<?php  /** @var $block \Vendor\Module\Block\MyCustomBlock */?>
<?php  $block->checkIfCustomerIsLoggedIn()) ?>

This works on Magento 2.4.6 . Good luck !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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