通过URL cakePHP传递多个,单个或无参数

Dom*_*mas 5 cakephp cakephp-2.0

所以我有以下控制器功能来添加事件:

public function add($id = null, $year = null, $month = null, $day = null, $service_id = null, $project_id = null){
...
}
Run Code Online (Sandbox Code Playgroud)

在某些情况下我需要做的是仅传递id和service_id或project_id并跳过年,月和日.我试图将参数作为空字符串或空值传递如下,但似乎没有一个工作.

echo $this->Html->link('Add event', array(
    'controller' => 'events',
    'action' => 'add',
25, null, null, null, 3, 54
))
Run Code Online (Sandbox Code Playgroud)

任何帮助深表感谢.

nIc*_*IcO 13

最简单的解决方案可能是使用查询参数.(我倾向于不再使用命名参数,因为CakePHP将很快或稍后删除它们)

视图:

echo $this->Html->link(__('add event'), array('controller' => 'events', 'action' => 'add', '?' => array('id' => 123, 'service_id' => 345, ...)));
Run Code Online (Sandbox Code Playgroud)

控制器:

public function add(){
    $id         = isset($this->request->query['id'])         ? $this->request->query['id']         : null;
    $year       = isset($this->request->query['year'])       ? $this->request->query['year']       : null;
    $service_id = isset($this->request->query['service_id']) ? $this->request->query['service_id'] : null;
    ...

}
Run Code Online (Sandbox Code Playgroud)

这样,只有一些参数很容易.