I am using Slim framework to create an REST API for my project. The WebApp itself is developed using ReactJS.
I have a doubt regarding the same. I have a set of values stored in an array in my ReactJS App. These values are filters which is used ti filter data from my table.
My table contains a field called location. In my WebApp I have search filter were I can add multiple locations based on which the results have to be displayed. I store these filters in an array.
I want to know how can I pass this array to Rest API Route so that I can get this array in my REST API Framework.
I want to achieve something like
/routes/location/[arrayOfValues]
There are only two ways of passing data to the server - either as URL params (for POST, GET, or anything else) or in the request body (for PUT or POST). Are you also writing the code to handle the API request? From your description of the use case, it sounds like GET would be the most semantically correct verb, so you you should use URL params as #BrianPutt suggested. Using square brackets is the standard way to encode an array value. How are the locations defined? Longitude/latitude? If so, you could also just build a comma-separated string, e.g.
/routes/location?q=12.3409,-40.0004,2.34004,120.0294
But odds are your back end framework has built-in support for parsing those URL params, so you should just use that.
/routes/location?q[]=12.3409,-40.0004&q[]=2.34004,120.0294
The only reason not to do this is if you have a huge number of locations, in which case you might want to use a POST request and build some kind of JSON object to put in the body.
Also: be consistent! If the rest of your API uses JSON, you should probably use it here too.
Related
I use last version of API Platform, and I was wondering if I could change the way to handle arrays in query string.
Default behavior relies on PHP way to handle arrays :
/customers?cars[]=audi&cars[]=mercedes&cars[]=bmw
According to Swagger documentation : https://swagger.io/docs/specification/serialization/, it can handle different ways :
/users?id=3,4,5
/users?id=3|4|5
Can I use this format using API Platform? I didn't find anything in the options, I suppose I can "trick" using Events (DeserializeListener maybe).
This input format seems supported by Swagger: the style name of the parameter is deepObject.
There are many ways to support multiple inputs in query because the query is in fact a simple string containing var=value, therefore you can imagine whatever you want since var may be duplicated. PHP use the array-style (or deepObject) approach by default, and this is fine.
API Platform as support for this kind of "multiple filter" query by default: https://api-platform.com/docs/core/filters/#search-filter
If you want to use the following approach: /users?id=3|4|5, you can define a custom filter with ease and use the request directly to explode the id parameter and complete your query internally.
is it possible in php to create a single url from multiple values across multiple tables?
For example, I want to generate a url that looks like the following:
http://www.domain.foo/joseph&soap&engineer&english, with the first name and surname is read from the NAMES table and occupation is read from the JOBS table and language is read from the SPEAKS table..?
Not sure if this is yet the correct way to solve my task, but thought I'd ask the question before I spend more time on it.
That isn't the way url parameters work. They should be name value pairs. You are also missing the query string. So what a normal url would look like would be this:
http://www.domain.foo/index.php?firstname=joseph&surname=soap&job=engineer&language=english
This is how you should be thinking about and approaching the use of the GET method and the passing of url parameters. Often this gets confused with URL rewriting, which converts url's that omit script names and parameters but you should start with the basic functionality and worry about layering on url rewriting once your code is working.
The other thing you might be missing is the "routing" that comes with pretty much every framework that implements the MVC pattern.
For example, consider route to be an additional parameter that gets passed as route=section_of_site. In your example, it seems that this function is going to look up information on a user or person, so perhaps it would be route=person
So now your full url would be:
http://www.domain.foo/index.php?route=person&firstname=joseph&surname=soap&job=engineer&language=english
Even without a framework or controller class, you now have a route variable which you could use to include a script, or to execute a function or class function. You have all the variables you need in $_GET, and all that is left is to do whatever data persistence work you require and return your result.
I'm currently working on a project that requires me to call an external API via HTTP to get some data in the form of JSON. This data will be saved to a database pattern defined with the Eloquent ORM. This API requires authentication (by token) and then accepts calls with GET, POST is not needed. The parameters are added to the URL (e.g. ?origin=LHR&destination=GHA).
I'm trying to find a Laravel way to access such data easily - just writing a basic class that has a function with all parameters doesn't seem right. I'd much rather have some sort of query builder, but for the URL. I looked at repositories, but that seems to be geared towards database calls.
To sum up: is there any good "Laravel way" to call an external API?
I've dene something like this just few days ago;
My solution was to define array GET params. F.a.
get-some-stuff?where[foo]=bar&with[relation]&with[otherRelation]
Then you can get the params via Input::get() and go through them with an foreach.
F.a.
foreach(Input::get() as $method => $value) { ...
In the foreach you could decide what to do based on $method
I have to create web page for testing and I don't know how to implement it's logic (because I don't want to break MVC). It will be created in CakePHP.
Base thing I want to do is, that presenters action will have a parameter (JSON object or JSON string) and based on this parameter, there will be created a testing form inside view.
After user submits the answer, it will be sent to my PHP algorithm as a parameter(type of JSON object or string). This algorithm will return another JSON object that will be used as parameter for above mentioned presenters action.
I don't know how to implement this logic of sending and receiving JSON in order of not breaking MVC rules. Please explain it to me.
The first thing to do is think about your design.
The controller can process your data and return a JSON response, so you'll want to use that. Luckily Cake has this built in. Have a read of the book, http://book.cakephp.org/2.0/en/views/json-and-xml-views.html
If you are passing JSON into your controller, you'll want to just pick it up out of the request. You'll be able to find it by debugging the request inside your controller. debug($this->request).
Then you can process the JSON in your controller, do some stuff and return a JSON response.
I have implemented a search form in Zend ( SOLR in the backend ).
To offer the user some more control I have added a multicheckbox for applying certain content filters.
The thing is in the following steps ( bear with me here ):
The request is made, something similar to /search/?q=bla&filter=1
The request is rewritten in the controller to be restful to: /search/query/bla/filter/1
If the user removes filter 1, adds filter 2 and submits the request becomes: /search/query/bla/filter/1?q=bla&filter=2
How would I properly create a restful request using that. The problem being here that using the getRequest() functionality of the Controller I will also get a value for the first filter, meaning I have no way of knowing which one I can discard.
So, to summarize:
How do I properly use a Zend_Form to create RESTful HTTP requests
IMO, you're trying to solve an inexistent problem. You're working on aesthetics not RESTful compliance.
REST tells that you need to identify resources, not that you're resource links should look nice (well in a way). But since your search is a kind of a filter, it is totally acceptable to use a QueryParam.
I think, your use of the term "search" within REST is wrong. Search is not a resource (at least in your case).
You should either hide the search implementation by doing something like:
GET /articles?q=foo&filter=1
The way you construct your filters criteria should not matter.
Or you may want to define a "search" as a resource (SO actually does that IIRC).
POST /articles/search/
"{
"query" : "foo",
"filter" : "bar"
}"
Would result in something like:
Location: /articles/search/a125f41fbc135d
Where a125f41fbc135d is the identifier of the search which would be used to identify the search resource.