Magento删除param并重定向到相同的url

时间:2014-01-15 14:26:49

标签: magento

我想删除一些URL参数(比如utm_source,但无论如何),把它放在会话中并重定向到同一页面,并使用此特定参数的URL清理

我在controller_front_init_before中完成了这样的事情:

$frontController = $observer->getEvent()->getFront();
$params = $frontController->getRequest()->getParams();
$myParams = array("b");
foreach($myParams as $myParam) {
    if (isset($params[$myParam])) {
        $customerSession->setData(
            $myParam, $params[$myParam]
        );
        unset($params[$myParam]);
    }
}
$frontController->getRequest()->setParams($params); // <- I don't know what to do with that

现在,在请求中重定向到同一页面的最佳方法是什么?

例如,将http://example.com?a=1&b=2&c=3重定向到http://example.com?a=1&c=3

谢谢!

1 个答案:

答案 0 :(得分:0)

$frontController = $observer->getEvent()->getFront();
$params = $frontController->getRequest()->getParams();
$shouldRedirect = false

foreach($params as $key => $value) {
    if ($key !== 'b') {
        $customerSession->setData($key, $value);   
    }
    else{
       unset($params[$key]);
       $shouldRedirect = true;
    }
}

if($shouldRedirect){
    //$url = get url and redirect
    //see http://magento.stackexchange.com/questions/5000/add-query-parameters-to-an-existing-url-string
    Mage::app()->getResponse()->setRedirect($url);       
}