0

I'm new to php and I'm using Slim Framework to develop a simple Rest Api.

According to docs, Slim uses FastRoute.

I have a route with optional params (vendor and quantity) like that:

path/items/{id}/{name}/{price}[/{vendor}[/{quantity}]]

The problem is that if I leave vendor blank the value of quantity goes to vendor and quantity will not be filled. I understand that with fast routes I can't achieve that. But, how can I do it?

  1. Can I do it with fast routes?
  2. Can I do it with Slim?
  3. Is there another framework that I can achieve what I want?
  4. Should I use only query strings? (I think that is the answer, but I need a help from someone with more knowledge).

Thank you.

Alan Godoi
  • 657
  • 1
  • 12
  • 39
  • I'm guessing that the quantity value is always numeric? And that vendors arent? Or is the vendor value an ID? If both can contain the same datatype (string or integer), then it can't be done it the way you're trying to do. It's impossible for the router to know if a value (let's say, 10) is a vendor ID or a quantity. Why not always send in the vendor? Or rather, why do you need all that information in the URL? Can't you get that from the first ID? – M. Eriksson Sep 22 '16 at 15:05
  • So, I can't do it, because it's an ID. How Can I pass these params? Can you give me an example/link? Thanks – Alan Godoi Sep 22 '16 at 16:01
  • I mean, think if I will add a new item with optional params. How can I do it? – Alan Godoi Sep 22 '16 at 16:20
  • Yes, I was just thinking abou that. I'll probably try query strings, I think it's better. Thank you for your help. If you can add a answer, I'll check it. Thanks, again. – Alan Godoi Sep 22 '16 at 17:22
  • Added my previous comment as an answer. – M. Eriksson Sep 22 '16 at 17:29

1 Answers1

1

If you are going to have optional parameters, which are of the same types and can be sent in any order, you could either use query strings, or have a fixed format where you add something like 0 instead of omitting it. Example where "vendor" isn't set:

/10/somename/15/0/12

Then the parameters aren't optional in the URL/route but you can handle that in your controller instead, just ignoring them if the param is a zero.

M. Eriksson
  • 13,450
  • 4
  • 29
  • 40