4

I'm trying to pass '?' and '=' symbols to a page using routing.yml

company.calc:
  path: '/form?company={nid}'
  defaults:
    _controller: FrameworkBundle:Redirect:redirect
    _title: 'Calc'
 requirements:
   _permission: 'access content'

but the symbols are transcoded to HTML-entities. It looks like form%3Fcompany%3D%3Fcompany%3D18141 and leads to page 404.

Any ideas how to pass the string like

/form?company=1234 

? This string is necessary to prepopulate the form's field.

Find the same question here but no reply, no solution :(.

  • This is the correct way, then they are decoded when the form receives the get parameters. – 4k4 Dec 13 '19 at 7:52
  • Yes, it is. But the path leads to page-404. If I replace the transcoded entities the page is good (not 404). Is there a way to switch off the transcoding? – Peter Lozovitskiy Dec 13 '19 at 8:05
  • 2
    I thought you want to pass '?' within a query parameter. You should change the question title to drupal.stackexchange.com/questions/229406/… – 4k4 Dec 13 '19 at 8:17
  • 1
    Don not specify query parameters in the path - element, check my answer below. – Rainer Feike Dec 13 '19 at 8:26
8

You declare query parameters in drupal routes in a mymodule.routing.yml file like this:

mymodule.mailcommitter:
  path: '/mailcheck/{uid}'
  defaults:
    _controller: '\Drupal\mymodule\Controller\ProfilController::checkMailCommit'
    _title: 'commit mail'
  options:
      query:
        token: ""
  requirements:
    _permission: 'access content'

Then, you build an url by route like so

'url' => Url::fromRoute('mymodule.mailcommitter', ['uid' => 1], ['query' => ['token' => 'hello']])

Also check this doc: https://www.drupal.org/docs/8/api/routing-system/parameters-in-routes/using-parameters-in-routes

  • Wow! You're my hero for the rest of the day!! – Peter Lozovitskiy Dec 13 '19 at 8:36
  • 3
    Great answer, you have to add the query parameter to the route when building the url. BTW I don't think the query option in the route definition has any effect, query parameters are transferred from the super globals to the request object and are available even before routing and are not checked in route matching. – 4k4 Dec 13 '19 at 8:41
  • @4k4 your right, the declaration in routing.yml makes no difference and options is a generic array in the route object - but it's nice to have a declaration anyway :-) – Rainer Feike Dec 18 '19 at 7:57

Not the answer you're looking for? Browse other questions tagged or ask your own question.