Passing optional information by URL is pretty easy in CakePHP. The Paginator does this all the time with the
Accessing the power of URL parameters, as CakePHP calls them, is slightly confusing at first. It’s not a method call or anything. Using
$this->params is the location for URL parameters.
If you run a
debug($this->params); you will find that there is a wealth of information stored there. To get the URL parameters, you need to look under the named parameters key:
Inside, you’ll get an array. Each key is the name of your parameter and each value is the value of the parameter, obviously.
So why are these at all useful? Imagine you had a listing page. By default, it would just list whatever was most recently added to the system. Sometimes though, you’d like to override that with a special parameter, like maybe a group parameter, where the list will show only things in the given group. Traditionally in CakePHP, one would have to use action arguments:
function list($arg1, $arg2, ...). Instead of being forced into a particular order and checking for null all over, URL parameters avoid all of that.
They are optional, so checking them is easy and since they’re named, you don’t need to worry about order.