So, we have this situation, this WEIRD and UNNECESARY situation, but our SEO team wants it, so we must swallow.
We need to pass our search filters as URL parts instead of just querystrings (which would be the obvious and normal thing).
Let’s say we have three filters
gender
age
and location
Well, like in any search with filters, we cann have all filters enabled or none.
In a normal scenario, this would be /search?gender=female&age=25&location=london
, on our strange scenario it ould be /search/female/25/london
But as I said we can have all or none /search
would be /search
(yep)
That’s easy, just a Controller
with a SearchAction($gender, $age, $location)
and a routing /search/{gender}/{age}/{location}
But let’s say, we only want to search female
and london
oops! our routing, controller and action won’t feet our needs, because {age}
is needed, and we aren’t using it.
We have the same problem if we try to search genderless people with 25 age
and located in london
well, it won’t work neither, we need a {gender}
/search/25/london
Of course, we don’t want to be able to load /search/25/london/female
because we want an order in our filters, but ordered so we don’t have duplicated content, you know, SEO stuff…
So that’s our situation, we need a controller’s action, which can handle n
parameters (we’ve count 5 by now) which will be our search filters, in a specific order but optional at the same time…
So now are a bit lost on this. We have defined that we want the order, we want the filters, and we want them to be optional, so the “easy” thing is that we know that the first parameter will always be the {gender}
but it can be optional, so if null
we will just ignore it on the DB query.
I do not provide code because what we’ve done is not even close to our goal, it’s just an array of optional parameters and a bunch of useless code, it was just a draft.
We can make a routing for each case, and let the hierarchy handle it if missing parameters, but of course what I am asking if there’s a possible and good looking way to do this using the less amount of lines possible, because you know, code must be good to maintain.
3
Answers
In the past i approach this problem with something like:
that could accept also a null –> any value like:
Hope this help
If you really have to do this (and you’re right that it sounds bad) then I would build it the way you think is right (with the optional parameters – let’s call this
searchAction
) but then create an action for each possible scenario and use the internal forwarding to direct to thesearchAction
For example you could have a route defined
search/{gender}/{age}
and which only matches when age is an integer. Then inside this action you could forward to your main search passing the arguments from your action as parameters to thesearchAction
.Then repeat for
/search/{gender}/{age}/{location}
,/search/{gender}/{location}
etc.May Be approached from the other side will be simplier.
You could make search route as ‘/search/allGender/allAge/allLocation’ for case without search word. If user want to search Age 25 it will be ‘/search/allGender/25/allLocation’…
Just ignore “reserved’ words (‘allGender’,’allAge’,’allLocation’) on the DB query.
I think your SEO team will be satisfied by this solution. And it’s very easy for realization