2

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
4

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');

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.