Magento 2 Programmatic Redirect Without Custom Controller

May 29th, 2020





While creating a custom program for my work, I attempted to figure out if there was a way to create a redirect from a custom external script to the cart.

Most StackOverflow questions had answers that essentially said to create your own extension of the controller and use the inbuilt _redirect() method.

After a bit of searching and combining some previous knowledge, I discovered that the ResponseInterface can be used to redirect natively.

$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
$redirect = $objectManager->get('\Magento\Framework\App\ResponseInterface');
$redirect->setRedirect('../checkout/cart')->sendResponse();

Using this, the client will be redirected one directory up and then to checkout/cart without needing a custom contrtoller.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *