Dmitry
Dmitry

Reputation: 345

How to describe route with complex parameter in Symfony 2

I have route: /user/username/{username}.{_format} And username could have any symbols. For example, all of the follows will be valid:

and so on...

each part of username could have any characters...

Also _format is optional string e.g json, xml ...

How I can describe it in my routing rules?

At now I have this:

MyBundle_getUserByUsername:
    pattern: /user/username/{username}
    defaults: { _controller: MyBundle:User:getUserByUsername, _format: json }
    requirements:
        _method: GET
        username: ".+"

But it not so useful as it could be. I can't specify format parameter...

I need your help guys...

Upvotes: 2

Views: 911

Answers (2)

james_t
james_t

Reputation: 2733

MyBundle_getUserByUsername:
    pattern: /user/username/{username}.{_format}
    defaults: { _controller: MyBundle:User:getUserByUsername, _format:json}
    requirements:
        _method: GET
        username: ".+(?<=.)"

Upvotes: 1

Vadim Ashikhman
Vadim Ashikhman

Reputation: 10126

Use the lazy quantificator

MyBundle_getUserByUsername:
    pattern: /user/username/{username}.{_format}
    defaults: { _controller: MyBundle:User:getUserByUsername, _format: json }
    requirements:
        username: ".+?"
        _method: GET
        _format: html|json

Upvotes: 0

Related Questions