Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

symfony2 use multiple url pattern for a single Controller Action using regular expression

Is that possible with symfony2 to define multiple url patterns for a single Controller Action using regular expressions, so we don't have to define several rules ? Thanks in advance

like image 650
svassr Avatar asked May 03 '12 20:05

svassr


2 Answers

When using annotations, you can define multiple routes. Like that:

/**
 * @Route ("item1")
 * @Route ("item/2")
 * @Method("GET")
 */
public function itemAction() {

}

I'm using Version 2.0.9

like image 143
mbo Avatar answered Sep 21 '22 21:09

mbo


Do you mean placeholders with requirements?

blog:
    pattern:   /blog/{page}
    defaults:  { _controller: AcmeBlogBundle:Blog:index, page: 1 }
    requirements:
        page:  \d+

Here you have multiple routes defined by a placeholder, validated by regular expressions going to the same controller action.

Edit:

Each part of the url can be a placeholder.

blog:
    pattern:   /{type}/{page}
    defaults:  { _controller: AcmeBlogBundle:Blog:index, page: 1 }
    requirements:
        type: blog|articles
        page:  \d+
like image 44
ilanco Avatar answered Sep 19 '22 21:09

ilanco