using Zend's default routing a URL looks like:
www.domain.com/controller/action/key1/value1/key2/value2/key3/value3
Each Key and Value are stored as a pair in the array returned by getParams();
In this example:
array("key1" => "value1", "key2" => "value2", "key3" => "value3")
I want the parameter URLs to look like:
www.domain.com/controller/action/value1/value2/value3
The parameters should be mapped in an array like this. The key should depend just on the value's position in the URL.
array(0 => "value1", 1 => "value2", 2 => "value3")
How can I do this?
You are going to need to read up a bit on ZF Routes. But essentially what you need to do is add something like this to your Bootstrap.php:
protected function _initRoutes()
{
$this->bootstrap('frontController');
$frontController = $this->getResource('frontController');
$router = $frontController->getRouter();
$router->addRoute(
'name_for_the_route',
new Zend_Controller_Router_Route('controller/action/:key1/:key2/:key3', array('module' => 'default', 'controller' => 'theController', 'action' => 'theAction', 'key1' => NULL, 'key2' => NULL, 'key3' => NULL))
);
}
The NULL's provide default values.
Then in your controller you will do something like this:
$key1 = $this->_request->getParam('key1');
$key2 = $this->_request->getParam('key2');
$key3 = $this->_request->getParam('key3');
or use the getParams method you previously mentioned.
You can also use PHP's array_values() function to create a numerically indexed array like so:
$numericArray = array_values($this->_request->getParams());
It is a very good idea to get into the habit of using routes as they provide abstraction between what the URI is and what controllers/actions get invoked. Essentially, what you can achieve with routes is object-oriented code that still makes perfect sense to a programmer, while at the same time a URI that makes perfect sense to a user.
I had the same intention because i want my URL to look something like this:
http://wwww.mypr.com/tours/Europe/spain/magicalJourney
I remember I can also Load everything from an .ini file, so i use this on my Bootstrap
public function _initRouter() {
$frontController = Zend_Controller_Front::getInstance();
$config = new Zend_Config_Ini(APPLICATION_PATH . '/configs/routes.ini');
$router = $frontController->getRouter();
$router->addConfig($config, 'routes');
}
Then I had this in my routes.ini
routes.tours.route = /tours/:group/:destination/:tour
routes.tours.defaults.module = default
routes.tours.defaults.controller = tours
routes.tours.defaults.action = handler
routes.tours.defaults.group = null
routes.tours.defaults.destination = null
routes.tours.defaults.tour = null
When you run $this->_request->getParams()
in your controller, this will produce something like:
Array ( [group] => Europe [destination] => Spain [tour] => MagicalJourney [module] => default [controller] => tours [action] => handler )
It actually works pretty well :)