从控制器重定向到页面锚点

时间:2013-10-10 13:41:01

标签: magento

我需要从控制器重定向到页面并滚动到特定锚点。我在普通的Magento控制器中使用以下内容

$this->_redirect("shop/boxes#".$mainSku);

假设$mainSku为123,则在末尾添加一个尾部斜杠,因此我将被重定向到http://example.com/shop/boxes/#123/。尾部斜杠将滚动效果丢失到锚点。

我怎样才能克服(以最干净的方式)?

3 个答案:

答案 0 :(得分:1)

这就是诀窍!

$url = Mage::getUrl("shop/boxes#".$mainSku);
$url = substr($url, 0, -1);
$this->getResponse()->setRedirect($url);

希望它对其他人有用。

答案 1 :(得分:1)

如何在getUrl方法之外添加锚名称?

$url = Mage::getUrl('shop/boxes').'#'.$mainSku;
$this->getResponse()->setRedirect($url);

答案 2 :(得分:0)

我遇到了同样的问题,Marius的答案是最好的解决方案,因为如果您使用参数或者使用添加密钥来解决Krt_Malta的解决方案并不起作用已启用网址 (对不起马吕斯,不能投票。)

我使用了原始$this->_redirect()代码并对其进行了修改,以便我可以在参数中为其指定锚点。

现在我可以这样使用它:
$this->_redirectAnhcor('*/*/edit', array('id' => $Id), '#products');

/**
 * Set redirect into response with anchor
 *
 * @param   string $path
 * @param   array $arguments
 * @param   string $anchor
 */
protected function _redirectAnhcor($path, $arguments = array(), $anchor = '')
{
    $this->_getSession()->setIsUrlNotice($this->getFlag('', self::FLAG_IS_URLS_CHECKED));
    $this->getResponse()->setRedirect($this->getUrl($path, $arguments) . $anchor);
    return $this;
}
相关问题