【问题标题】:Passing multiple, single or none parameters through URL cakePHP通过 URL cakePHP 传递多个、单个或无参数
【发布时间】:2014-05-05 09:53:24
【问题描述】:

所以我有以下用于添加事件的控制器功能:

public function add($id = null, $year = null, $month = null, $day = null, $service_id = null, $project_id = null){
...
}

在某些情况下我需要做的是只传递 id 和 service_id 或 project_id 并跳过年、月和日。我尝试将参数作为空字符串或 null 传递如下,但似乎都不起作用。

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

非常感谢任何帮助。

【问题讨论】:

标签: cakephp cakephp-2.0


【解决方案1】:

最简单的解决方案可能是使用查询参数。 (我倾向于不再使用命名参数,因为 CakePHP 迟早会放弃它们)

查看:

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

控制器:

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;
    ...

}

这样很容易只有一些参数。

【讨论】:

    【解决方案2】:

    不要将它们作为/var/var/var 传递,而是使用 URL 变量:

    www.whatever.com?id=123&month=4
    

    然后访问它们:

    $id = $this->request->query['id'];
    $month= $this->request->query['month'];
    ... etc
    

    请先检查它们是否已设置或不为空...等等,但是 - 似乎更适合您的目标。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-08-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多