Search code examples
phpsymfony1routessymfony-1.4

symfony 1.4 routing: match routes using URL parameters


I have set up friendly URLs for a few search result pages, using a custom route for each:

friendly_search_resultpage:
  url: /products/my-friendly-alias
  param:
    module: products
    action: search
    querystring: searchattribute
    querystring2: searchattribute2

This works fine, but when doing the search directly (i.e. browsing to /products/search?querystring=search...) I want to set a <link rel="canonical"> containing the matching friendly URL. This will help Google understand the relation and that there isn't duplicate content.

I put my friendly URL route at the top of routes.yml and hoped for a magic match, but URL parameters aren't recognised in the checking done by symfony. I have dug into sfRoute, with no luck. Is it possible?


Solution

  • I ended up writing a custom routing class to use for these routes, which is executed when url_for() is called. Here is the code:

    <?php
    class mySearchFriendlyRoute extends sfRoute
    {
      public function matchesParameters($params, $context = array())
      {
        // I can't find symfony sorting parameters into order, so I do
        // (so that foo=x&bar=y is treated the same as bar=y&foo=x)
        ksort($params);
        $mine = $this->defaults;
        ksort($mine);
        if ($params == $mine) {
          return true;
        }
        return false;
      }
    }
    

    To use, add class: mySearchFriendlyRoute to the routes.yml entry.