1

I want to redirect the user to specific page in Magento 2 from PHTML file.

In Magento 1 we use following method to redirect

Mage::app()->getFrontController()->getResponse()->setRedirect(Mage::getBaseUrl().'?login=false');`

but not finding any solution for Magento 2.

I have also read this blog but it shows to create a custom module for that. As I don't want to do this from the module.

Please provide any solution if available.

Edit

I just want to redirect the user to the specific page if not log in for that to create module it will be a not a good thing.

2
  • 1
    Honestly, write a module. The template is not the right place for this. It should only contain most basic rendering logic May 20, 2017 at 6:42
  • @DavidVerholen Can we do this without module.
    – Rahul
    May 20, 2017 at 6:45

1 Answer 1

5

Add this code in your phtml file....

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$redirect = $objectManager->get('\Magento\Framework\App\Response\Http');
$redirect->setRedirect('Your path....');
5
  • 2
    okey ... Happy Coding !!!! @RahulDambare May 20, 2017 at 7:24
  • Its gives The page isn’t redirecting properly error when redirecting to homepage or customer/account/login
    – Rahul
    May 20, 2017 at 8:20
  • 1
    use abc/ ......instead of abc/index/index May 20, 2017 at 9:05
  • 3
    For customer account login = > $redirect->setRedirect($this->getBaseUrl()."customer/account/login"); May 20, 2017 at 9:06
  • @RohanHapani, how to redirect to custom folder in root path with the params from phtml files? Feb 8, 2019 at 8:10

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.