Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magento 2 Cannot redirect from admin controller to front end

I am trying to use this code to redirect to a frontend URL, but it always redirects to the admin dashboard. I searched for and tried a lot of demo code, but I can't get it to work. I assume all attributes were set correctly.

 $url= $this->_storeManager->getStore(1)->getUrl('storelocator/index/index');
 $resultRedirect = $this->resultRedirectFactory->create();
 $resultRedirect->setUrl($url);
 return $resultRedirect; 
like image 218
long thanh Avatar asked Nov 23 '25 07:11

long thanh


1 Answers

From here:

public function __construct(
    ...
    \Magento\Store\Model\StoreManagerInterface $manStore,
    ...
) {
    ...
    $this->manStore = $manStore;
    ...
}

public function execute()
{
    ...
    $resultRedirect = $this->resultRedirectFactory->create();
    $route = 'customer/account'; // w/o leading '/'
    $store = $this->manStore->getStore();
    $url = $store->getUrl($route, [$parm => $value]); // second arg can be omitted 
    $resultRedirect->setUrl($url);
    return $resultRedirect;
}
like image 123
Alex Gusev Avatar answered Nov 26 '25 19:11

Alex Gusev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!