1

I have a Drupal site with a specialised search query page that accepts about a dozen arguments as URL query parameters, most of which are optional.

Simplified routing YML file looks like this:

my_search_results_page.content:
  path: '/my-search'
  defaults:
    _controller: '\Drupal\my_search\Controller\MySearchResultsController::content'
    _title: 'Search Results'
    location: null
    type: 'any'
    status: 'any'
    filter: 'any'
  requirements:
    _permission: 'access content'
    _access: 'TRUE'

The url query args location, type, status and filter are passed into the controller along with a bunch of others that I've ommitted for brevity. Location is a required field, hence the null default. All of the others are optional, and will get the value any if not specified in the query. This all works perfectly.

Now I have an additional requirement, to be able to create custom SEO-friendly preset URLs for specific search queries. For example, if they want to do a marketing push for widgets in London, they want a URL that looks like /widgets-in-london, which would resolve to a search results page where location is set to London and type is set to Widgets.

This seems like it could be achieved relatively easily by adding an additional routing entry to the my_search.routing.yml file for each preset URL they want, and and some extra code in my controller class to manage it. But the marketing team want to be able to create and edit these URLs on the fly, which makes a code-based solution non-optimal. But I'm at a loss as to how to do it more generally.

Can anyone help me come up with a flexible solution that will allow them to set up custom routes without them having to touch the code.

Thank you.

  • Sounds like a job for the Redirect module – Hudri Apr 17 at 12:19
  • @Hudri - thanks for the suggestion. It does sound like what I want. Silly question: Does the redirect module mean that the page is redirected? Our marketing team want the final URL of the page to remain as the custom URL they specified, not just a redirect back to the page with query args. (they've been very specific about this, because of "mumble mumble SEO reasons") – Spudley Apr 17 at 12:30
  • Isn't this an alias then? You can add one manually in /admin/config/search/path/add. Programmatically you can fill the alias table like the module Pathauto does for content or implement your own path processor. – 4k4 Apr 17 at 12:51
  • @4k4 - Yes it should be. But unfortunately, URL alias doesn't work with URLs that include query args. – Spudley Apr 17 at 12:53
  • OK, then you need a custom path processor. For example: PathProcessorFiles. This transfers the file name into a query arg because the file name with slashes doesn't work as route parameter. – 4k4 Apr 17 at 13:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.