Routing, an unlimited number of parameters

空扰寡人 提交于 2019-12-19 02:55:05

问题


For example, link:

/shop/phones/brend/apple/display/retina/color/red

where:

phones    - category alias
brend     - name of attribute;   apple    - attribute value
display   - name of attribute;   retina   - attribute value
color     - name of attribute;   red      - attribute value

Attributes can be any number. Order may also be different.

The beginning of the route is clear:

/shop/{category}

And what to do next is unclear.

In symfony 1, a set at the end star ("/shop/:category/*") and all that was not clearly marked, and come in a pair of

name -> value

Question: how to describe the route in symfony 2?


回答1:


The route:

my_shop:
  pattern: "/{path}"
  defaults: { _controller: "MyShopBundle:Default:shop" }
  requirements:
    path: "^shop/.+"

and then you could just parse the $path in the controller:

class DefaultController extends Controller {
...
    public function shopAction($path) {
        // $path will be 'shop/phones/brend/apple/display/retina/color/red'
        ...
    }
...
}


来源:https://stackoverflow.com/questions/11415965/routing-an-unlimited-number-of-parameters

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!