2016-07-28 47 views
3

我想使用此代碼重定向到前端網址,但它總是重定向到管理儀表板。我搜索並嘗試了很多演示代碼,但我無法實現它的工作。我假設所有屬性都設置正確。Magento 2無法從管理控制器重定向到前端

$url= $this->_storeManager->getStore(1)->getUrl('storelocator/index/index'); 
$resultRedirect = $this->resultRedirectFactory->create(); 
$resultRedirect->setUrl($url); 
return $resultRedirect; 

回答

-1

我有類似的問題。在我的情況下解決方案是相當微不足道的。在網址的開頭添加斜槓並沒有訣竅。

$result = $this->resultRedirectFactory->create(); 
    // Pay attention to leading slash in url. 
    $result->setUrl('/training_render/layout/onepage'); 
    return $result; 

感謝

0

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

這裏是讓店面URL的解決方案:

/* @var \Magento\Framework\ObjectManagerInterface $objectManager */ 
/* @var \Magento\Store\Api\Data\StoreInterface $store */ 
$scopeResolver = $objectManager->create('Magento\Framework\Url\ScopeResolverInterface', [ 'areaCode' => \Magento\Framework\App\Area\Area::AREA_FRONTEND ]); 
$url = $objectManager->create('Magento\Framework\Url', [ 'scopeResolver' => $scopeResolver ])->setScope($store)->getUrl('xxx/xxx/xxx', [ '_scope_to_url' => false, '_nosid' => true ]); 
+0

爲你打破不要使用直接對象管理器調用DI概念。 – Luke

相關問題