Magento 2无法从管理控制器重定向到前端

时间:2016-07-28 08:48:53

标签: redirect magento2

我正在尝试使用此代码重定向到前端网址,但它始终会重定向到管理信息中心。我搜索并尝试了很多演示代码,但我无法让它工作。我假设所有属性都设置正确。

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

3 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分:0)

以下是获取店面网址的解决方案:

/* @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 ] );

答案 2 :(得分:-1)

我有类似的问题。在我的案例中解决方案相当简单。在URL的开头添加斜杠就可以了。

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

由于