Accessing CakePHP named vars and PHP GET vars and difference between the two
What is the difference between /action?query=value
and /action/query:value
as the latter seems to be the way开发者_如何学Python query strings are handled in CakePHP and how do I do either the latter or the former in Cake?
Thanks
Example of regular php:
...action.php?name=blah&id=7
You can access this like:
$name = $_GET['name'];
$id = $_GET['id'];
Example with named parameters in CakePHP:
...action/name:blah/id:7
And you can access them like this:
$name = $this->params['named']['name'];
$id = $this->params['named']['id'];
UPDATE: It's no longer recommended to use Named Parameters in CakePHP as they have been removed in CakePHP 3.0+
Benefits of using named parameters in CakePHP:
- Full router support (see @deceze comment below explaining this)
- makes it easier/cleaner when combining with things like Paginate
- better for SEO (depending on what you're passing)
- you're in CakePHP - use CakePHP stuff <-- he says somewhat jokingly
- ...and more?
Side note:
You can also sent parameters via url/CakePHP without using named parameters:
...action/blah/7
These are retrieved by function vars:
function action($name, $id) {
In the first case, you can access the query string parameters the same way you would in vanilla PHP:
foreach ($_GET as $param => $value){
// do stuff
}
I've never seen the second method used, but my best guess is that those come through as extra parameters to the controller action and can be indexed by their key similar to $_GET
. That's a total guess, though.
精彩评论