通过URL cakePHP传递多个、单个或无参数
因此,我有以下用于添加事件的控制器函数:通过URL cakePHP传递多个、单个或无参数,cakephp,cakephp-2.0,Cakephp,Cakephp 2.0,因此,我有以下用于添加事件的控制器函数: public function add($id = null, $year = null, $month = null, $day = null, $service_id = null, $project_id = null){ ... } 在某些情况下,我需要做的是只传递id和服务id或项目id,并跳过年、月和日。我曾尝试按如下方式将参数作为空字符串或null传递,但似乎都不起作用 echo $this->Html->link('Add
public function add($id = null, $year = null, $month = null, $day = null, $service_id = null, $project_id = null){
...
}
在某些情况下,我需要做的是只传递id和服务id或项目id,并跳过年、月和日。我曾尝试按如下方式将参数作为空字符串或null传递,但似乎都不起作用
echo $this->Html->link('Add event', array(
'controller' => 'events',
'action' => 'add',
25, null, null, null, 3, 54
))
非常感谢您的帮助。不要将它们作为
/var/var/var
传递,只需使用URL变量:
www.whatever.com?id=123&month=4
然后访问它们:
$id = $this->request->query['id'];
$month= $this->request->query['month'];
... etc
可以先检查它们是否已设置为空……等等,但是-似乎更适合您的目标。最简单的解决方案可能是使用查询参数。(我倾向于不再使用命名参数,因为CakePHP将很快或稍后删除它们) 视图: 控制器:
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;
...
}
这样,只需要一些参数就很容易了。如果使用命名参数而不是传递参数,就很容易了。
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;
...
}