I want to do redirect after do function from model. I already tried both code below, both works redirect, but both return error too.

Redirect 1 :


Redirect 2 :

$resultRedirect = $this->resultRedirectFactory->create();
return $resultRedirect->setPath('*/*/addresses');

Is there another way to redirect from model without returning error?


No worries! We also can redirect from Model.

Just try the following code in your model:

Add below code as parameter in __construct method of your Model

\Magento\Framework\UrlInterface $url, 
\Magento\Framework\App\ResponseFactory $responseFactory,

Then next, Create object for both as below

$this->_url = $url;
$this->_responseFactory = $responseFactory;

Now whatever your method in this model, use following code to redirect

$CustomRedirectionUrl = $this->_url->getUrl('REDIRECTION URL HERE');

Don't Forgot to put exit at the end!


| improve this answer | |
  • It's works, thank you!!! :) but you should change '$this->request = $request;' to '$this->_responseFactory = $responseFactory;' but still, thank you for your answer!!! :) – Edwin Widhiyanto Sep 27 '17 at 3:32
  • Yes @user58086 Sorry! I have updated the answer. :) – Irfan Momin Sep 27 '17 at 3:53
  • But, Is this correct using exit(); function? – Ramanathan Aug 24 '19 at 10:27

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.