I’ve discovered an interesting behavior in CakePHP that, as far as I know, is not documented in the book. I’d rather not attempt to edit the book – I’m not sure I’m right and anyway, they’ve just changed the book in some rather annoying way that I’d like to observe a bit before using – so instead I’ll just note it here for now.
The behavior in question concerns the pass array that exists in the Controller $this->params array and also the one that exists in the Router::connect function. Apparently, these two are connected in at least one very specific way. Namely: the order of elements in the Router::connect pass array overrides the order of elements in the URL model. Consider the following:
* Entry in routes.php:
array(‘controller’ =>; ‘rating_events’, ‘action’ =>; ‘index’),
array( ‘pass’ => array(‘company’, ‘location’, ‘city’),
<– Insert regular expressions here –>
In the above code, if the pass array was not set,we would expect that the pass array in $this->params[‘pass’] would follow the natural order in the URL:
array(‘ny’, ‘east_rochester’, 12);
But with the ‘pass’ array set in Router::connect, the array actually comes out like this:
array(12, ‘ny’, ‘east_rochester’);
You can therefore define the exact order of elements in that resulting params array. I can see this being handy as in the development process I’m currently going through, where the order and number of parameters passed in this way might be different from Controller to Controller, but always referring to much of the same data and using a lot of shared functions. Being able to organize the order of this array – for example, always having city and location appear in the same order and at the same numeric position – lowers the cost of processing and developing later when you don’t need to make exceptions in your code for Controller A versus Controller B.
I happened upon this when reorganizing my URL structure to try to make it universal. I couldn’t get the params[‘pass’] array to work, so I printed it out. Imagine my surprise when it was a completely different order than I expected!