2
Magento\Framework\App\Action

Redirect method is defined in Magento controller interface

/**
 * @var \Magento\Framework\App\Response\RedirectInterface
 */
protected $_redirect;

So i am aware about that if i use controller Action then it will work.

But i want use specific model method to redirect the url

File location

app/code/[Vendor]/[Module]/Model/TestManagement.php

and function goes like :

    public function Test(){
        $this->_redirect('rbvendor');
        return;
    }
1

You can use below code for the redirect from the model.

Add below code as the parameter in the __construct method of your Model

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

Create object for both as below

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

Now, modify your function code.

public function Test()
{
   $CustomRedirectionUrl = $this->_url->getUrl('rbvendor/index/index');
   $this->_responseFactory->create()->setRedirect($CustomRedirectionUrl)->sendResponse();
   exit();
}

Please let me know if you face any issue.

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.