Reputation: 12433
I have routing like this.
acme_apiByDate_homepage:
pattern: /api/byDate/{date}/{page}/{limit}
defaults: { _controller: AcmeTopBundle:Api:byDate,date:"",page:0,limit:50, _format: xml }
normal URL is like this
/api/byDate/2013-04-12/0/40
However sometimes I would like to omit the date.
But, this shows error
/api/byDate//0/40
I know I can omit the last parameter, but I want to omit the middle parameter.
How can I make it?
Upvotes: 0
Views: 1378
Reputation: 5877
You can change route parameters like that:
acme_apiByDate_homepage:
pattern: /api/byDate/{page}/{limit}/{date}
defaults: { _controller: AcmeTopBundle:Api:byDate, page:0, limit:50, date: null, _format: xml }
And add in your controller:
byDateAction($page, $limit, $date = null)
{
}
If null for route and controller doesn't work try an empty string.
UPDATE:
You can also define more routes for same action like:
api_data_by_date:
pattern: /api/byDate/{date}/{page}/{limit}
defaults: { _controller: AcmeTopBundle:Api:getData, page:0, limit:50, date: null, _format: xml }
api_data_by_page:
pattern: /api/byPage/{page}/{date}/{limit}
defaults: { _controller: AcmeTopBundle:Api:getData, page:0, limit:50, date: null, _format: xml }
api_data_by_limit:
pattern: /api/byLimit/{limit}/{page}/{date}
defaults: { _controller: AcmeTopBundle:Api:getData, page:0, limit:50, date: null, _format: xml }
Or, you could add some requirements for parameters, example:
api_data_by_date:
pattern: /api/getData/{date}/{limit}/{page}
defaults: { _controller: AcmeTopBundle:Api:getData, page:0, limit:50, date: 2013-04-12, _format: xml }
requirements:
date: [0-9]{4}\-[0-9]{2}\-[0-9]{2}
page: \d+
limit: \d+
Or try:
api_data_by_date:
pattern: /api-d-{date}-l-{limit}-p-{page}
defaults: { _controller: AcmeTopBundle:Api:getData, page:0, limit:50, date: 2013-04-12, _format: xml }
requirements:
date: [0-9]{4}\-[0-9]{2}\-[0-9]{2}
page: \d+
limit: \d+
Upvotes: 2