6

I have tried a few things and I cannot manage to make this working.

My route is the following.

    amu_hal.publicationsPerYear:
      path: '/publications/{year}'
      defaults:
        _controller: '\Drupal\amu_hal\Controller\AmuHalController::publicationPerYear'
        _title: 'publicationPerYear'
        year: 2016
        route_parameters:
          url: 'default'
      requirements:
        _permission: 'access content'

The code for the controller is the following.

    public function publicationPerYear($year,$url) {
      $url=urldecode($url);
      $url .='&fq=producedDateY_i:' . $year;

      try {
        $client = new Client();
        $response = $client->request('GET', $url);
        $content = json_decode($response->getBody(), true);
        // ...
      }
    }

The request http://mysite/publications/2016?url=https%3A%2F%2FmyWS%2Fsearch%2F%3Frows%3D5%26sort%3DproducedDate_tdate%2Bdesc%26fl%3Dtitle_s%2Cen_title_s%2Clabel_s%2Cen_label_s%2CdocType_s%2CauthIdHal_s%2ChalId_s%2CstructId_i%2Curi_s%2Ckeyword_s%2Cen_keyword_s%2CauthLastNameFirstName_s%2CjournalTitle_s%26fq%3DauthFullName_t%3A(%22M%22+OR+%22e+d%22) gives the following error:

RuntimeException: Controller "Drupal\amu_hal\Controller\AmuHalController::publicationPerYear()" requires that you provide a value for the "$url" argument (because there is no default value or because there is a non optional argument after this one). in Drupal\Core\Controller\ControllerResolver->doGetArguments() (line 165 of /var/aegir/platforms/drupal8-dev/core/lib/Drupal/Core/Controller/ControllerResolver.php).

  • 1
    Is the yaml format valid? yamllint.com - always worth asking. – Kevin Feb 22 '17 at 15:59
  • nice tool , yes it is valid – Matoeil Feb 22 '17 at 16:05
5

You cannot create a route based on the query string, but you still can retrieve query string values in your code:

amu_hal.publicationsPerYear:
  path: '/publications/{year}'
  defaults:
    _controller: '\Drupal\amu_hal\Controller\AmuHalController::publicationPerYear'
    _title: 'publicationPerYear'
    year: 2016
  requirements:
    _permission: 'access content'
public function publicationPerYear($year) {
  $url = \Drupal::request()->query->get('url') ?: 'default';
}
  • as a note i have found the yml 'route_parameters ' in the core menu_test.links.menu.yml !? – Matoeil Feb 22 '17 at 16:52
  • As I wrote, you should let get request object get injected, not use \Drupal::request() – Berdir Feb 22 '17 at 18:49
  • Does injection has any other purpose than unit testing? I don't really understand why Drupal's static services must not be used. – zessx Feb 22 '17 at 18:58
4

I don't think we support passing a query argument through like that.

Just get the request object with Request $request and then get the url from there with $request->query->get('url');


Sample method

  /**
   * @param \Symfony\Component\HttpFoundation\Request $request
   */
  public function download(Request $request) {
    dd($request->query->get('url'));
  }
2

Meanwhile (Drupal 8.6) it's possible to declare query parameters in drupal routes. In a mymodule.routing.yml file, to declare a query variable "token", do it 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']])

The result is an uri like /mailcheck/1/?token=hello

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

  • Where did you find the query option for *.routing.yml in the linked doc? I could find it only for *.links.menu.yml, which would be another option to build URLs like in your code example: drupal.org/docs/8/api/menu-api/… – 4k4 Dec 16 '19 at 8:13
  • @4k4 - you are right again. The link is misleading for the query-option topic. – Rainer Feike Dec 18 '19 at 7:59

Your Answer

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

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