将 urlencoded URL 作为参数传递给 CakePHP 的控制器/操作
我对 CakePHP 还很陌生,正因为如此,我以前用 Zend Framework 做的一些基本事情被 Cake 打败了。
我正在开发一个项目,我必须将命名参数传递给控制器/操作。设置路由并传递参数相当简单,我的问题是当参数是 urlencoded url 时。
例如:http://www.cakephp.com/ controller/action/http%3A%2F%2Fwww.google.com 无论控制器和操作设置如何,都会抛出 404,但传递 /controller/action/http://www.google.com 会起作用在某种程度上,唯一的问题是它将 http 标识为命名参数。换句话说,如果我这样做 /controller/action?url=http://www.google.com 它将起作用。
我为此使用的解决方法是将值作为 base64 编码字符串传递,但它带来了一些限制。例如,如果它是一个API,则无法保证使用该API的系统可以对字符串进行base64编码。
无论如何,最好的解决方案仍然是将 url 编码的字符串传递给命名参数。问题是,为什么 CakePHP 不接受 urlencoded 字符串作为参数,并且为什么它会抛出 404?
预先感谢大家。
I'm fairly new on CakePHP and because of so, there are some basic things that I used to do with Zend Framework that I'm beaten up with Cake.
I'm working on a project where I have to pass a named parameter to a controller / action. Setting up the route and passing the parameter is fairly simple, my problem is when the parameter is a urlencoded url.
For example: http://www.cakephp.com/controller/action/http%3A%2F%2Fwww.google.com regardless of the controller and action setup, will throw a 404, but passing /controller/action/http://www.google.com work in some way, the only problem is that it identifies the http as a named parameter. In another way, if I do /controller/action?url=http://www.google.com it will work.
The work around that I had used for this is to pass the value as a base64 encoded string, but it brings some limitations. For instance, if it is an API, there is no way that you can guarantee that the system using the API can encode base64 a string.
Anyway the best solution would be still passing a url encoded string to a named parameter. Question is, why CakePHP does not accept a urlencoded string as a parameter and why does it throws a 404?
Thanks all in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我已经添加了解决此问题的方法。之前指向帖子的答案实际上回答了发生这种情况的原因以及解决方案之一。所发生的情况是,Apache 上 .htaccess 的解决方法有点危险,因为它会禁用安全标准。
有两种方法可以通过代码解决这个问题(我正在使用两种方法):
它远不是一个漂亮的解决方案,但它绝对是一个实用的解决方案。
I have added a work around this issue. The previous answer that pointed to a post actually answered why it was happening and one of the solutions. What happens is that the workaround for .htaccess on Apache is a bit dangerous because it will disable a security criteria.
There are 2 ways to work this out via code (and I'm using both):
It is far from being a beautiful solution, but it is definitely a practical one.
我在 Cakephp 4.x 中偶然发现了同样的问题,
显然您可以使用
**
创建自定义路由,这将禁用默认的 urldecoding。解决问题。所以现在我将一个
base64_encode(Security::encrypt($val))
值放入Router::url()
函数中。默认情况下,这将对参数进行 url_encode,使其成为有效/工作 url。Cakephp 默认会进行 urldecode,这很好。但它做了两次?如果存在
/
,则会导致它拆分参数。这不好。所以在我的routes.php 中我添加了:
有点烦人,但它现在可以工作了。在 4.x 中工作起来就像一个魅力,花了我整个下午的时间。把这个留在这里以防其他人遇到这个问题。 (以及未来的我)。
来源:https://github.com/cakephp/cakephp/issues/4723#问题评论-56912905
I stumbled upon this same problem in Cakephp 4.x
Apparantly you can create a custom route with
**
that will disable the default urldecoding. Fixing the problem.So right now I throw a
base64_encode(Security::encrypt($val))
value into theRouter::url()
function. This will url_encode the params by default so it becomes a valid/working url.Cakephp then urldecodes by default, which is good. But it does it twice? Causing it to split up the params if there is a
/
present. Which isn't good.So in my routes.php I added:
Kinda annoying how this works, but it works now. Works like a charm in 4.x and cost me the entire afternoon. Just leaving this here in case anyone else has this problem. (and for future me).
Source: https://github.com/cakephp/cakephp/issues/4723#issuecomment-56912905