1 Comment

Magento 2: how to redirect from an admin controller

Merge Guest cart with Logged in - Rest API`
Check if product with sku exists in Magento 2

In Magento 1, we were able to do the following directly in a controller action method :

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

Or in a more generic way:

$this->_redirect('adminhtml/controller/action');

What’s the equivalent in Magento 2 ?

Related Posts

Merge Guest cart with Logged in - Rest API`
Check if product with sku exists in Magento 2

1 Comment on this article

  • User November 12, 2016

    To do the following in Magento 2, you must do the following in your execute method:

    public function execute()
    {
        /* Do your controller action stuff here */
        $resultRedirect = $this->resultRedirectFactory->create();
        $resultRedirect->setPath('adminRouteFrontName/controllerFolder/actionClass');
        return $resultRedirect;
    }
    

    You don’t need to add the MagentoFrameworkControllerResultRedirectFactory to your constructor as it is already declared in the abstract class MagentoFrameworkAppActionAbstractAction so you have already access to the $this->resultRedirectFactory variable.

    The parameters in my example are:

    • adminRouteFrontName: declared in your adminhtml/routes.xml
    • controllerFolder: name of the controller folder under Controller/Adminhtml
    • actionClass: name of your action class under the controller folder for example with Index.php it would be index, for Grid.php it would be grid and so on.

    You can also still use the * as we used to do in M1.

    You can also use parameters for example like this:

    $resultRedirect->setPath('catalog/*/', ['store' => $storeId, '_current' => true, 'id' => $id]);
    

    Reply

Add a comment