Cakephp中传递参数的不同方法
我正在使用 cakephp v1.26。
我在控制器中有一个像这样的函数:
class testingsController extends AppController{
function testing($id=null){
$recieved = $id;}
}
我不确定是否有更好的方法将参数传递给操作测试。
但我在一些网站上找到了这两种方法。
以下参数传递方式有什么区别吗?
1. url/testings/testing/1
2. url/testings/testing:1
I am using cakephp v1.26.
I got a function in a controller like this:
class testingsController extends AppController{
function testing($id=null){
$recieved = $id;}
}
I am not sure if there are any better ways to pass a parameter to the Action testing.
But I have come across some web sites and got these two methods.
Is there any difference in the following parameter passing methods?
1. url/testings/testing/1
2. url/testings/testing:1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用标准路由,这将调用
TestingsController::testing(1)
。这是标准参数传递,任何超出
/:controller/:action/
的参数都会“按原样”传递给被调用的操作。/controllers/action/param1/param2
对应ControllersController::action($param1, $param2)
使用标准路由,这将调用
TestingsController::index()
和将
$this->params['named']['testing']
设置为1
。这称为命名参数。命名参数可以按任何顺序传递。这两个 URL 是等效的:
url/testings/testing:1/foo:2
url/testings/foo:2/testing:1
它们不会被传递给函数,如
functiontesting($id = null)
>。$id
将为null
。它们仅在$this->params['named']
数组中可用。With standard routes, this will call
TestingsController::testing(1)
.This is standard parameter passing, any parameters beyond
/:controller/:action/
are passed "as-is" to the called action./controllers/action/param1/param2
corresponds toControllersController::action($param1, $param2)
With standard routes, this will call
TestingsController::index()
andset
$this->params['named']['testing']
to1
. This is known as a named parameter.Named parameters can be passed in any order. These two URLs are equivalent:
url/testings/testing:1/foo:2
url/testings/foo:2/testing:1
They will not be passed to the function, as in
function testing($id = null)
.$id
will benull
. They're only available in the$this->params['named']
array.第一个示例将其作为数字参数传递,
第二个示例将传递命名对,就像数组一样,
您可以将其用于不同的用途。您会注意到分页器在对列和页面进行排序时使用
key:val
配对参数。书中有一些进一步的信息, http: //book.cakephp.org/2.0/en/development/routing.html#passed-arguments
The first example you have will pass it as a numeric parameter
The second will pass a named pair, rather like an array
You can use either for different things. You'll notice that the paginator uses
key:val
paired parameters when sorting columns and pages.There is a little bit of further info in the Book, http://book.cakephp.org/2.0/en/development/routing.html#passed-arguments