在CakePHP中将参数传递给方法
我的Posts Controller中有以下方法:在CakePHP中将参数传递给方法,php,cakephp,Php,Cakephp,我的Posts Controller中有以下方法: public function index( $filter = null ) { debug($filter); exit; } $this->params['named']['filter'] 如果我访问以下URL:/posts/index/filter:test它将返回null,但是如果我使用/posts/index/test它将返回test 我的印象是,Cake默认将传递的参数视为命名 我尝试使用如下路线解决此问题:
public function index( $filter = null )
{
debug($filter); exit;
}
$this->params['named']['filter']
如果我访问以下URL:/posts/index/filter:test
它将返回null,但是如果我使用/posts/index/test
它将返回test
我的印象是,Cake默认将传递的参数视为命名
我尝试使用如下路线解决此问题:
Router::connect('/test',array('controller'=>'posts','action'=>'index','filter'=>'test')代码>
但它仍然返回为空!好像筛选器没有传递给方法
如何修复此问题?如果需要获取命名参数,可以使用控制器的params属性:
public function index( $filter = null )
{
debug($filter); exit;
}
$this->params['named']['filter']
但是它甚至是一个命名参数吗?当您像这样将它传递到url时:/index/filter:value-它是命名的。那么为什么我的方法不拾取它呢?因为您的第一个参数$filter
是传递的参数编号1(正如文档中所述,并且一直如此)