URI Parameters to Array
Zend_Controller_Router_Route_Module uses a very simple mapping to determine the name of the controller and the name of the action within that controller:
http://framework.zend.com/controller/action/
Notice above that the first segment is always the name of the controller and the second segment is always the name of the action. Optionally, parameters may be defined in the URI that will be passed to the controller. These take the form of key/value pairs:
http://framework.zend.com/controller/action/key1/value1/
Now, lets convert those parameters defined in the URI to an array. Why? Errr, just for fun :)
<?php
function convertUriParamsToArray($uri, $action)
{
// Parse url
$uriParts = parse_url($uri);
// Convert string to array
$uriPathArray = explode('/', $uriParts['path']);
// Search for the selected segment
$selectedSegment = array_search($action, $uriPathArray);
if ($selectedSegment !== false) {
$uriPathArray = array_slice($uriPathArray, $selectedSegment+1);
}
// Total key/value pairs
$totPairs = ceil(count($uriPathArray) / 2);
$key = 0;
$newArray = array();
for ($i = 0; $i < $totPairs; $i++) {
$newArray[$uriPathArray[$key]] = (isset($uriPathArray[$key+1]))
? $uriPathArray[$key+1]
: '';
$key += 2;
}
return $newArray;
}
$uri = 'http://www.mysite.com/controller/action/key1/value1/key2/value2';
$action = 'action';
$uriArray = convertUriParamsToArray($uri, $action);
print_r($uriArray);
Return Values
Array ( [key1] => value1 [key2] => value2 )
Converting to object
$uriArray = (object) $uriArray; print $uriArray->key1;
Return Values
value1