10

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?

19

You can directly use below code in Action controller

$this->_redirect('customer/account/');

16

Use below code:

protected $resultRedirect;

public function __construct(\Magento\Framework\Controller\ResultFactory $result){
   $this->resultRedirect = $result;
}

public function execute()
{
    $resultRedirect = $this->resultRedirect->create(ResultFactory::TYPE_REDIRECT);
    $resultRedirect->setUrl($this->_redirect->getRefererUrl());

    return $resultRedirect;         
 }  

Hope it will helpful to you and for others.

  • you my friend is now my god, I pray to you every night! Thank you! Saved me heaps of time. – OZZIE May 23 '18 at 13:08
9
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

2

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;
        }
    }
2
$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 :)

  • 1
    Using the $this->_url->getUrl() was the missing detail for me, thanks! – cjohansson Jun 8 '18 at 7:08
0

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;
    }
}

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.