I added a new custom tab in customer dashboard. If my custom form is successfully executed i need to redirect to customer main page customer/account
. How can I do this?
8 Answers
You can directly use below code in Action controller
$this->_redirect('customer/account/');
Use below code:
protected $resultFactory;
public function __construct(
\Magento\Framework\Controller\ResultFactory $resultFactory,
\Magento\Framework\App\Action\Context $context
)
{
$this->resultFactory = $resultFactory;
parent::__construct($context);
}
public function execute()
{
$redirect = $this->resultFactory->create(\Magento\Framework\Controller\ResultFactory::TYPE_REDIRECT);
$redirect->setUrl('/redirect/to/destination');
return $redirect;
}
Hope it will helpful to you and for others.
-
2you my friend is now my god, I pray to you every night! Thank you! Saved me heaps of time.– OZZIEMay 23, 2018 at 13:08
public function execute()
{
$resultRedirect = $this->resultRedirectFactory->create();
$resultRedirect->setPath('routename/controllerpath/controllername');
return $resultRedirect;
}
No need to declare $this->resultRedirectFactory in construct as its auto declared in construct of \Magento\Framework\App\Action\Action to which your custom controller should extend. Otherwise you'll get error during compilation: resultRedirectFactory is already declared
Try this code :
namespace Company\Module\Controller\Index;
use Magento\Framework\Controller\ResultFactory;
class Actionname name extends \Magento\Framework\App\Action\Action
{
public function execute()
{
$resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
// Your code
$resultRedirect->setUrl($this->_redirect->getRefererUrl());
return $resultRedirect;
}
}
$resultRedirect = $this->resultRedirectFactory->create();
$this->messageManager->addNotice(__("Login Reuqire For Edit Post. So Please <i class='fa fa-lock'></i> Login Now And Edit Your Post."));
$customerBeforeAuthUrl = $this->_url->getUrl('customer/account/login', array('referer' => $objecturl->getEncodedUrl($this->_url->getUrl(''))));
return $resultRedirect->setPath($customerBeforeAuthUrl);
Try this code, i hope you can use it :)
-
1Using the
$this->_url->getUrl()
was the missing detail for me, thanks! Jun 8, 2018 at 7:08
To Redirect to specific URL bellow code works fine for me. Please check this out
public function execute()
{
$this->getResponse()->setRedirect($url);
}
Here, $url is full url. e.g. http://yoursite.com/customer/account/create/
-
This is very useful thanks, much easier to use in Controllers, and with
$this->_redirect->getRefererUrl()
as URL you can easily redirect to referer on error. Dec 14, 2021 at 10:14
Try this code in controller for redirect to previous page :
<?php
namespace Vendor\Module\Controller\Index;
class Index extends \Magento\Framework\App\Action\Action {
protected $_pageFactory;
/**
* @var \Magento\Framework\Session\SessionManagerInterface
*/
protected $_sessionManager;
public function __construct(
\Magento\Framework\App\Action\Context $context
) {
$this->resultFactory = $context->getResultFactory();
return parent::__construct($context);
}
public function execute()
{
$resultRedirect = $this->resultRedirectFactory->create();
$url = $this->_redirect->getRefererUrl();
$resultRedirect->setUrl($url);
return $resultRedirect;
}
}
-
Assigned
$this->resultFactory
in the constructor but not using it in the execute method. Dec 24, 2021 at 4:04
Try this code
use Magento\Framework\Controller\ResultFactory;
public function execute()
{
$resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
.........
.........
$resultRedirect->setPath('customer/account');
return $resultRedirect;
}