Many developers require a redirect URL. So we can have created this blog so that the developer can easily understand the redirect URL.
In this blog, we are going to understand how to set redirect URL in Magento 2. You can use two ways to set URL: setPath & setUrl .
⇒ Use setPath to Redirect URL:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
public function __construct( \Magento\Framework\Controller\Result\RedirectFactory $resultRedirectFactory ) { $this->resultRedirectFactory = $resultRedirectFactory; } public function RedirectUrl(\Closure $proceed) { if () { // if the condition is satisfied. URl work continues return $proceed(); } else { //if the condition is not satisfied. URL is redirected to any page return $this->resultRedirectFactory->create()->setPath(‘checkout/cart’); } } |
⇒ Use setUrl to Redirect URL:-
1 2 |
$URL = “https://demo.mv132/test/”; return $this->resultRedirectFactory->create()->setUrl($URL); |
currently, we are passing a static URL.you can pass as par requirement
⇒Using objectManager :-
1 2 3 4 5 |
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $resultRedirectFactory = $objectManager->get(‘\Magento\Framework\Controller\Result\RedirectFactory’); $resultRedirectFactory->create()->setPath(‘checkout/cart’); |
I hope you understand our blog and if you still have any problems you can let us know in the comments section.
Thank You !!!..