magento _redirect 参数带有 + 或/ [英] magento _redirect with parameters that have + or /

查看:23
本文介绍了magento _redirect 参数带有 + 或/的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎是对

的调用

$this->_redirect('*/*/myaction',$myargs);

没有正确转义参数所以如果

$myargs=array(p1=>'包含 + 或/的字符串')

创建的 URL 将类似于:

 ..../myaction/?p1/string%20that%20has%20+%20or%20/%20within%20it

导致操作上的 getParams 集合具有p1 的值为 'string that has or ' <- 缺少加号且值已损坏和在其中"没有价值或类似的东西.

在将参数传递给 _redirect 之前,我应该如何处理这些参数?

艾尔

解决方案

是的,有两种标准方式.

  1. 将所有参数作为路由参数传递,但使用 php urlencode() func 对它们进行编码:<代码><预>foreach ($myargs as $key => $val) {$myargs[$key] = urlencode($val);}$this->_redirect('*/*/myaction', $myargs);

  2. 将您的参数作为查询参数传递<代码><预>$this->_redirect('*/*/myaction', array('_query', $myargs));

您最好采用第二种方法,因为逻辑上您的参数不是路由而是查询参数.Magento 是用大量的架构思想制作的,所以它通常会指出更好的做事方式 - 这就是为什么在你的情况下使用第二种方式发送参数更容易.

注意:_redirect() 在内部使用 Mage_Core_Model_Url,所以这个答案中所说的一切都适用于所有其他 url 形成例程和 Url 模型的所有用法.

seems like a call to

$this->_redirect('*/*/myaction',$myargs);

does not properly escape the arguments so if

$myargs=array(p1=>'string that has + or / within it')

the created URL will be something like:

 ..../myaction/?p1/string%20that%20has%20+%20or%20/%20within%20it

causing the getParams collection on the action to have p1 with value 'string that has or ' <- plus sign missing and value broken and ' within it' with no value or something similar.

is there any standard way I should handle the arguments before passing them to _redirect ?

Eyal

解决方案

Yes, there are two standard ways.

  1. Pass all your params as route params, but encode them with php urlencode() func:

    foreach ($myargs as $key => $val) {
        $myargs[$key] = urlencode($val);
    }
    $this->_redirect('*/*/myaction', $myargs);
    

  2. Pass your params as query params

    $this->_redirect('*/*/myaction', array('_query', $myargs));
    

You'd better take second approach, because your params logically are not route but query parameters. Magento is made with a lot of architecture thinking, so it usually points better ways to do stuff - that's why in your case it's easier to send params using second way.

Notice: _redirect() internally uses Mage_Core_Model_Url, so everything said in this answer is true for all other url-forming routines and all usages of Url model.

这篇关于magento _redirect 参数带有 + 或/的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆