TLDR: 对于 CakePHP 2.x:是使用 params['named'] 还是 params['url'] 仅取决于您要查找的数据。 'url' 返回域后整个 url 的字符串,'named' 返回一个数组,其中包含任何传递的“命名”变量(逗号分隔的键:值对)
CakePHP 3.x:没有“命名”变量
更深入的解释:
了解为什么要使用其中一个的最佳方法是在视图中调试参数:
调试($this->params);
您会看到,params 数组中有很多数据。例如,使用我的网址:http://www.example.com/dashboards/index/1/2/blah:test
params => array(
'plugin' => null,
'controller' => 'dashboards',
'action' => 'index',
'named' => array(
'blah' => 'test'
),
'pass' => array(
(int) 0 => '1',
(int) 1 => '2'
),
'models' => array(
'Dashboard' => array(
'plugin' => null,
'className' => 'Dashboard'
),
//...
)
)
data => array()
query => array(
'dashboards/index/1/2/blah:test' => ''
)
url => 'dashboards/index/1/2/blah:test'
base => ''
webroot => '/'
here => '/dashboards/index/1/2/blah:test'
如您所见,它包含大量数据。仅通过查看数据即可解释您“为什么要使用“命名”与“网址”的问题。
$this->params['url'] 返回字符串'dashboards/index/1/2/blah:test'(在大多数情况下不是很有用)。
$this->params['named'] 返回命名变量的键/值数组(在这种情况下,只有一个变量):array('blah' => 'test');(如果这是我们正在寻找的更有用)
所以...答案是,如果您想要命名变量,请使用 'named' - 如果您想要将 URL 的结尾作为字符串,请使用 'url'。