将 urlencoded URL 作为参数传递给 CakePHP 的控制器/动作 [英] Passing an urlencoded URL as parameter to a controller / action at CakePHP

查看:21
本文介绍了将 urlencoded URL 作为参数传递给 CakePHP 的控制器/动作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 CakePHP 还很陌生,因此,我曾经用 Zend Framework 做的一些基本事情被 Cake 打败了.

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.

我正在处理一个项目,我必须将命名参数传递给控制器​​/操作.设置路由和传递参数相当简单,我的问题是当参数是一个urlencoded url.

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.

例如: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 它将起作用.

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.

我为此使用的解决方法是将值作为 base64 编码字符串传递,但这会带来一些限制.比如,如果是API,就无法保证使用该API的系统可以将base64编码为字符串.

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.

无论如何,最好的解决方案仍然是将 url 编码的字符串传递给命名参数.问题是,为什么 CakePHP 不接受 urlencoded 字符串作为参数,为什么会抛出 404?

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?

提前致谢.

推荐答案

我添加了一个解决此问题的方法.指向帖子的先前答案实际上回答了它发生的原因以及解决方案之一.发生的情况是 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):

  1. 将所有网址作为 base64 编码字符串发送
  2. 接受 url 作为命名参数,但是,您会注意到,它将任何 http://转换为 http:/,因此有必要正确识别何时发生这种情况,然后才更正字符串.

这远不是一个漂亮的解决方案,但绝对是一个实用的解决方案.

It is far from being a beautiful solution, but it is definitely a practical one.

这篇关于将 urlencoded URL 作为参数传递给 CakePHP 的控制器/动作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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