CakePHP允许使用斜杠作为命名参数值 [英] CakePHP allow forward slash as named parameter value
问题描述
我正在使用cakePHP,陷入困境。我想将命名参数传递给包含正斜杠的操作。例如
I am using cakePHP and got stuck in a situation. I want to pass a named parameter to my action that contains forward slashes. For example
http://www.test.com/claim/index/user:rLbu78h2RpVwLTy/bki3pK1NkkXhCCaYfQ/zXDIfZR4=
基本上是 rLbu78h2RpVwLTy / bki3pKYZRQR4 是一个参数,但蛋糕只能识别出它的第一部分,直到第一个斜杠。
Basically "rLbu78h2RpVwLTy/bki3pK1NkkXhCCaYfQ/zXDIfZR4=" is one parameter but cake is only recognizing the first part of it until the fist slash.
打印命名的参数数组时,我得到:
When printing the named params array I get:
Array
(
[user] => rLbu78h2RpVwLTy
)
如何我可以转义斜杠并允许蛋糕接受它作为命名参数的一部分吗?
How can I escape the forward slash and allow cake to accept it as part of the named parameter?
谢谢
推荐答案
不要使用命名参数
可以使用命名参数( / foo / this-is:a-named-parameter /
),最好坚持使用普通的url position / path参数和/或 GET
参数。随着时间的流逝,事实证明,使用命名参数传递信息不是一个很好的解决方案。
Don't use named-parameters
While you can use named parameters (/foo/this-is:a-named-parameter/
), it's best to stick to normal url positional/path arguments and/or GET
arguments. Over time it has proven not to be a great solution to pass around information using named parameters.
如果您修改了您正在使用的网址格式如下:
If you modify the format of the url you're using to be e.g.:
/claim/index/<user token>
然后,您可以使用追踪星空路线以捕获在 / index /
之后在单个变量中发生的所有事件:
Then you can use a trailing star route to captuer everythiing that occurs after /index/
in a single variable:
Router::connect(
'/claim/index/**',
array('controller' => 'claims', 'action' => 'index')
);
通过这种方式, / index / $ c $之后的内容无关紧要c>您会在索引操作中收到它:
In this way it doesn't matter what comes after /index/
you'll receive it in your index action:
// Request to /index/rLbu78h2RpVwLTy/bki3pK1NkkXhCCaYfQ/zXDIfZR4=
function index($user) {
// $user === 'rLbu78h2RpVwLTy/bki3pK1NkkXhCCaYfQ/zXDIfZR4='
}
这篇关于CakePHP允许使用斜杠作为命名参数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!