5

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 :(.

4
  • This is the correct way, then they are decoded when the form receives the get parameters.
    – 4uk4
    Dec 13, 2019 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? Dec 13, 2019 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/…
    – 4uk4
    Dec 13, 2019 at 8:17
  • 1
    Don not specify query parameters in the path - element, check my answer below. Dec 13, 2019 at 8:26

1 Answer 1

12

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

3
  • Wow! You're my hero for the rest of the day!! Dec 13, 2019 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.
    – 4uk4
    Dec 13, 2019 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 :-) Dec 18, 2019 at 7:57

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