Search code examples
zend-frameworkzend-routezend-controller-router

ZF: Custom route, make all parts as param


Hopefully someone can help me out here.

I have a Zend Router that handles adding stuff to the basket. Now i'm building a customiser module, so the user can add as many parts, in different colors, on specified sections of the product.

My basic url is like this

http://www.domain.com/shop/add/custom/size/type

My current router.ini is like this

resources.router.routes.shopAddCustom.route = shop/add/custom/:size/:type/*
resources.router.routes.shopAddCustom.defaults.module = shop
resources.router.routes.shopAddCustom.defaults.controller = order
resources.router.routes.shopAddCustom.defaults.action = add
resources.router.routes.shopAddCustom.defaults.product = builder

What i would really like to acommodate are urls like this.

http://www.domain.com/shop/add/custom/size/type/part3/blue/right/part2/part6/both/part7/red/part1/left/orange/

Basicly the everything behind size/type/ is a part, a color or a section (right, left, both)

How can i get a single array of all url-path-parts after size/type?

array(0 => 'part3', 1 => 'blue', 2 => 'right', 3 => 'part2', 4 => 'part6' [...] );

If i just use $this-_request->getParams(); i get an array like this

array('part3' => 'blue', 'right' => 'part2', 'part6' => 'both' [...] );

I could run through that array, adding all keys and values as values in a new array. Problem is, if the number of url-path-parts is odd the last part will not be returned to the params, as it is seen as a empty variable, thus not added to the params array.

Any ideas is much apreciated :)


Solution

  • Well, this is how i think i could do this - Any other solutions are still welcome!

    $size = $this->_getParam('size');
    $type = $this->_getParam('type');
    
    $baseRouterUrl = $this->_helper->url->url(array('size' => $size, 'type' => $type), 'shopAddCustom', true);
    $pathInfo = dirname($this->_request->getPathInfo() . '/.');
    
    $pathInfo = str_replace($baseRouterUrl, '', '/' . $pathInfo);
    $pathInfo = trim($pathInfo, '/\\');
    
    $pathArr = explode('/', $pathInfo);
    

    This is the resulting array.

    array(11) {
      [0]=>
      string(5) "part3"
      [1]=>
      string(4) "blue"
      [2]=>
      string(5) "right"
      [3]=>
      string(5) "part2"
      [4]=>
      string(5) "part6"
      [5]=>
      string(4) "both"
      [6]=>
      string(5) "part7"
      [7]=>
      string(3) "red"
      [8]=>
      string(5) "part1"
      [9]=>
      string(4) "left"
      [10]=>
      string(6) "orange"
    }