简体   繁体   中英

Optional prefix for route in Symfony2

I want to these URLs all match same controller:

/
/7
/articles
/articles/
/articles/7

Is it possible to add optional prefix to symfony's routing so the article prefix be optional?
How?

You can create 2 routeds pointing to the same controller:

acme_article_prefix:
    path: /articles/{id}
    requirements:
        id: \d+
    defaults: { id: 5 }

acme_article:
    path: /{id}
    requirements:
        id: \d+
    defaults: { id: 5 }

Another option is to make the prefix a placeholder too:

acme_article:
    path: /{_prefix}/{id}
    defaults: { _prefix: articles, id: 5 }
    requirements:
        id: \d+
        _prefix: articles

for the framework, there is a patch for a long time to implement routes like /(articles/){id} , which will match the urks you set above.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM