4

I've spent the past few weeks building a RESTful API in PHP (Laravel framework). Also I am building a JavaScript frontend (jQuery / Backbone) that uses this API.

In this API there are some GET requests that require a fair amount of query parameters, most of them are arrays of IDs. I have structured it like this:

/api/cars?colors[]=1&colors[]=3&makes[]=6&makes[]=2&sold=false

(E.g. color with ID=1 is red, make with ID=6 is Audi, etc.)

In PHP the parsing of an array from $_GET['colors'] works out of the box. Also, when I use

$.ajax({
  url: "someurl",
  method: "GET",
  data: {
    cars: theCarsArray,
    makes: theMakesArray,
    sold: false
  }
});

Seems to work perfectly as well (the arrays are send in the URL as described above). I am wondering: is this the accepted approach of sending arrays of IDs in a REST interface? Are there any drawbacks to this approach? What are the alternatives?

1 Answer 1

2

In my experience, that's the accepted way of passing array values to a PHP script. And since you're using PHP, you should leverage what's there already. That said, array values are not generally passed that way when the web page is implemented in other programming languages/frameworks. In some cases, you would see this:

/api/cars?colors=1&colors=3&makes=6&makes=2&sold=false

In those languages/frameworks, they often provide methods for accessing the request parameter as a scalar or as an array. So you're exposing your implementation. If you want to hide the fact that you're using PHP, you can change it to the above, but then you'd have to add additional functionality to parse your request parameters. Like I said though, since you're implementing this in PHP, I don't see anything wrong with taking advantage of what it offers out of the box.

My only suggestion would be to change your url endpoint to /api/cars/ since you're returning a collection of cars (presumably) and that seems to be the more RESTful approach.

Anyway, just my two cents. Hope it's helpful.

Sign up to request clarification or add additional context in comments.

1 Comment

Is that syntax really PHP specific? Looks like Rails uses this approach as well? rails.nuvvo.com/lesson/6371-action-controller-parameters

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.