Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CakePHP 3.0 query string parameters vs passed parameters

In CakePHP 3.0 named parameters have been removed (thank god) in favour of standard query string parameters inline with other application frameworks.

What I'm still struggling to get my head around though is that in other MVC frameworks, for example ASP.NET you would pass the parameters in the ActionResult (same as function):

Edit( int id = null ) {

    // do stuff with id

}

And that method would be passed the id as a query string like: /Edit?id=1 and you'd use Routing to make it pretty like: /Edit/1.

In CakePHP however anything passed inside the function parameters like:

function edit( $id = null ) {

    // do stuff with $id

}

Must be done as a passed parameter like: /Edit/1 which bypasses the query string idea and also the need for routing to improve the URL.

If I name the params in the link for that edit like:

$this->Html->link('Edit', array('action' => 'edit', 'id' => $post->id));

I then have to do:

public function edit() {

    $id = $this->request->query('id');

    // do stuff with $id

}

To get at the parameter id passed. Would of thought it would pick it up in the function like in ASP.NET for CakePHP 3.0 but it doesn't.

I prefer to prefix the passed values in the edit link instead of just passing them so I don't have to worry about the ordinal as much on the other end and I know what they are etc.

Has anyone played with either of these ways of passing data to their methods in CakePHP and can shed more light on the correct ways of doing things and how the changes in version 3.0 will improve things in this area...

like image 307
Cameron Avatar asked Jan 10 '14 00:01

Cameron


1 Answers

There are a few types of request params in CakePHP 3.0. Let's review them:

The Query String: are accessed with $this->request->query(), are not passed to controller functions as arguments and in order to make a link you need to do Html->link('My link', ['my_query_param' => $value])

Passed arguments: The special type of argument is the one that is received by the controller function as an argument. They are accessed either as the argument or by inspecting $this->request->params['pass']. You Build links with passed args depending on the route, but for the default route you just add positional params to the link like Html->link('My link', ['action' => view, $id, $secondPassedArg, $thirdPassedArg])

Request Params: Passed arguments are a subtype of this one. A request param is a value that can live in the request out of the information that could be extracted from the route. Params can be converted to other types of params during their lifetime.

Consider this route:

Router::connect('/articles/:year/:month/:day', [
  'controller' => 'articles', 'action' => 'archive'
]);

We have effectively created 3 request params with that route: year, month and day and they can be accessed with $this->request->year $this->request->month and $this->request->day. In order to build a link for this we do:

$this->Html->link(
  'My Link',
  ['action' => 'archive', 'year' => $y, 'month' => $m, 'day' => $d]
);

Note that as the route specify those parameters, they are not converted as query string params. Now if we wanted to convert those to passed arguments, we connect this route instead:

Router::connect('/articles/:year/:month/:day',
  ['controller' => 'articles', 'action' => 'archive'],
  ['pass' => ['year', 'month', 'day']]
);

Our controller function will now look like:

function archive($year, $month, $day) {
  ...
}
like image 105
José Lorenzo Rodríguez Avatar answered Sep 20 '22 18:09

José Lorenzo Rodríguez