2

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 :

$this->_redirect('*/*/addresses');

Redirect 2 :

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

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

3

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');
$this->_responseFactory->create()->setRedirect($CustomRedirectionUrl)->sendResponse();
exit();

Don't Forgot to put exit at the end!

Cheers!!!

  • 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

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.