skip to Main Content

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


  1. In the past i approach this problem with something like:

    /search/gender/{gender}/age/{age}/location/{location} 
    

    that could accept also a null –> any value like:

     /search/gender/male/age/any/location/london
    

    Hope this help

    Login or Signup to reply.
  2. 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 the searchAction

    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 the searchAction.

    return $this->forward(
      'MyBundle:Search:search', [], ['gender' => $gender, 'age' => $age]
    );
    

    Then repeat for /search/{gender}/{age}/{location}, /search/{gender}/{location} etc.

    Login or Signup to reply.
  3. 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search