Passing multiple, single or no parameters via cakePHP URL

so I have the following controller function to add events:

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

In some cases I only need to do id and service_id or project_id and skip year, month and day. I tried passing parameters as empty strings or zeros as shown below, but none of them worked.

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

Any help is greatly appreciated.

+4
source share
2 answers

The simplest solution is likely to use query parameters. (I no longer want to use named parameters, as CakePHP will delete them soon)

View:

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

Controller:

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

}

Thus, it would be easy to have only some parameters.

+12
source

, /var/var/var, URL:

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

:

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

, ... .., ... , , .

+1

Source: https://habr.com/ru/post/1539254/


All Articles