Zend URL - hide key and show value

using default Zend routing, the URL looks like this:

www.domain.com/controller/action/key1/value1/key2/value2/key3/value3

Each key and value is stored as a pair in the array returned getParams();. In this example:

array("key1" => "value1", "key2" => "value2", "key3" => "value3")

I want the parameter urls to look like this:

www.domain.com/controller/action/value1/value2/value3

Parameters should be displayed in such an array. The key should depend only on the position of the value in the URL.

array(0 => "value1", 1 => "value2", 2 => "value3")

How can i do this?

+4
source share
2 answers

You need to read a little ZF Routes . But basically you need to add something similar 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))
    );
}

NULL provides 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');

getParams, .

PHP array_values ​​() , :

$numericArray = array_values($this->_request->getParams());

, , , URI / . , , , - - , , URI, .

+1

, , URL :

http://wwww.mypr.com/tours/Europe/spain/magicalJourney

, .ini, 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');
}    

.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

$this->_request->getParams() , - :

Array ( [group] => Europe [destination] => Spain [tour] => MagicalJourney [module] => default [controller] => tours [action] => handler )

:)

+2

All Articles