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.

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

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....');
  • 2
    okey ... Happy Coding !!!! @RahulDambare – Rohan Hapani May 20 '17 at 7:24
  • Its gives The page isn’t redirecting properly error when redirecting to homepage or customer/account/login – Rahul May 20 '17 at 8:20
  • 1
    use abc/ ......instead of abc/index/index – Rohan Hapani May 20 '17 at 9:05
  • 3
    For customer account login = > $redirect->setRedirect($this->getBaseUrl()."customer/account/login"); – Rohan Hapani May 20 '17 at 9:06
  • @RohanHapani, how to redirect to custom folder in root path with the params from phtml files? – jafar pinjar Feb 8 at 8:10

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.