If you liked what you've learned so far, dive in!
Subscribe to get access to this tutorial plus
video, code and script downloads.
Similar to pagination is filtering. Let's say that I want to be able to search
in the programmers collection by nickname. So I'll add a &nickname=2
.
Why 2? Well, my programmer nicknames aren't very interesting, but if I use
2, I'll want it to return Programmer2 and Programmer12. Of course right now,
this doesn't have any affect yet.
Filtering is actually really easy. But the reason I wanted to cover it is
that sometimes people wonder if they should get clever with their URLs when
they're filtering and maybe come up with URLs like /api/programmers/filter/nickname2
.
Don't do that. If you're filtering, use a query parameter, end of story.
So how do we get this to work? It couldn't be simpler. At the top of listAction
,
I'll look for the nickname
query parameter. And if it's present, we'll
query in a special way. And if it's not, we'll do the normal findAll
.
I have a shortcut setup to query using MySQL LIKE. I'll pass it the value
surrounded by the percent signs:
... lines 1 - 89 | |
public function listAction(Request $request) | |
{ | |
$nicknameFilter = $request->query->get('nickname'); | |
if ($nicknameFilter) { | |
$programmers = $this->getProgrammerRepository() | |
->findAllLike(array('nickname' => '%'.$nicknameFilter.'%')); | |
} else { | |
$programmers = $this->getProgrammerRepository()->findAll(); | |
} | |
... lines 99 - 123 | |
} | |
... lines 125 - 214 |
And that should be it!
If we go back to the Hal Browser and hit go, we get nothing back! But we're
actually on page 3, so click to go back to page 1. Hmm, now we have too many results!
That's because we lost the &nickname
query parameter. That's because I was
lazy - if I have extra filters I should pass those to the 3rd argument
of PaginatedRepresentation
. If I do that, it'll show up in our pagination
links.
I'll re-add &nickname=2
manually and this time, we see it's only returning
Programmer2 and Programmer12, and it knows that there's only going to be
one page of them. So that's it for filtering: use query parameters, do whatever
logic you need for filtering, and pass the filter to PaginatedRepresentation
,
even though I didn't do it here. It's that easy.
One quick thing to notice is that even though we now support this nickname
filter, there isn't any way for the API client to know this just by looking
at the links. We have links to help them go through the pages, but no link
that says: "Hey, if you want, you could pass a nickname query parameter to
filter this collection". There's not really a way with HAL to do that. There
are other formats that give you more options and ways to say "Hey, you can
add the following 4 query parameters to this URI, and here's what each will
do".
And this is one of those spots where REST can get frustrating. The purpose of adding links is to help make your API client's life easier. They are not intended to replace human-readable documentation. So even if you did find a clever way to include filtering information in your response, this would just be as a "nice feature" for your API. You should still document which endpoints have which filters and what they mean.
The term HATEOAS means: hypermedia as the engine of application state. And as cool as it is, it's at the heart of this confusion. In its most pure form, HATEOAS is an idea that seems to suggest that if your API has really good links, it doesn't need human-readable technical documentation. Instead, a client can follow the links in your response and other details you return to figure everything out.
But this is a dream, not a reality. As cool as our API is, it lacks a ton
of details. For example, it doesn't have any information about filtering.
Second, it doesn't tell you which HTTP methods each URL supports, nor what
fields you should POST to /api/programmers
in order to create a new resource.
The links we have are nice, but they're nowhere near giving the API client
everything it needs.
So think of links as a nice addition to your API, but not something that'll replace really nice human documentation.
Everything is possible of course :). But, there's no simple way to do this that I can think of - you would probably build some custom functionality to do this yourself. So, it depends on how badly you need this!
Hi I wanted to let you know that adding the filter prameter in the created links like you did cause two problems :
1 - if you have the prameter page in the link sent by the client you'll have two page prameter in the link you generate in your code
2 - this add in the generated link the filter prameter with the syntax : %5Bfilter%5D=ilterValue
The solution i found is to do it like this :
$createLink = function ($pageumber) use ($routeName, $routeParams,$request) {
return $this->router->generate($routeName, array_merge(
$routeParams,
$request->query->all(),
array("page" => $pageumber)
));
};
And don't forhget to write the array("page" => $pageumber) after $request->query->all() otherwise the page prameter in the query will override the $pageNumber in the merged array!!
PS : You do great work really great
// composer.json
{
"require": {
"silex/silex": "~1.0", // v1.3.2
"symfony/twig-bridge": "~2.1", // v2.7.3
"symfony/security": "~2.4", // v2.7.3
"doctrine/dbal": "^2.5.4", // v2.5.4
"monolog/monolog": "~1.7.0", // 1.7.0
"symfony/validator": "~2.4", // v2.7.3
"symfony/expression-language": "~2.4", // v2.7.3
"jms/serializer": "~0.16", // 0.16.0
"willdurand/hateoas": "~2.3" // v2.3.0
},
"require-dev": {
"behat/mink": "~1.5", // v1.5.0
"behat/mink-goutte-driver": "~1.0.9", // v1.0.9
"behat/mink-selenium2-driver": "~1.1.1", // v1.1.1
"behat/behat": "~2.5", // v2.5.5
"behat/mink-extension": "~1.2.0", // v1.2.0
"phpunit/phpunit": "~5.7.0", // 5.7.27
"guzzle/guzzle": "~3.7" // v3.9.3
}
}
Would it be possible to somehow dynamically add the parameters exposed by the serializer as (the only) valid search fields?