I know I can pass variables to templates using render arrays.

$theme = 'block-AcademicDisplay';   

return array(
  '#theme' => $theme,
  '#content' => $content,
  '#url' => urlencode($url),

How can I pass variables and call controller methods from that template? On that template, when clicking on some buttons or links, I would like to call a controller method and pass it 2 parameters.

  • 1
    Hi @Matoeil Can you please share your solution if resolved? Mar 13, 2019 at 15:41
  • i have used parameters upcasting in routes ( see below)
    – Matoeil
    Mar 15, 2019 at 8:38

2 Answers 2


Passing arguments in route parameters

I think the missing part is the route that you have defined for the controller. You have to use this to generate the url:

$url = Url::fromRoute('entity.node.canonical', ['node' => 1]);

This example is for the node controller and passes the argument nid=1. The parameter names are defined in the route definition.

Passing arguments in query parameters

I've seen in one of your other posts that you want to use query parameters. This is an example for the user login:

$url = Url::fromRoute('user.login', [], ['query' => ['destination' => 'http://example.com']]);

For this to work you don't need to define a query parameter in the route. Simply inject the request as function argument (the request is always available to a controller, it's not necessary to define it in the route) and get the query parameter:

use Symfony\Component\HttpFoundation\Request;

  public function myController(Request $request) {

    $destination = $request->query->get('destination');


i have used parameter upcasting in routes to convert dynamic values in url to object instances


in a bloc template listing all publications

 <a href="/publication/{{ row.halId_s }}">call to publication detail</a>

my_module.routing.yml , linking the route to the controller, with upcasting parameters

#parameter upcasting
  path: '/publication/{halId_s}'
    _controller: '\Drupal\my_module\Controller\MyModuleController::publicationDetail'
    _title: 'publication detail'
        type: int
    _permission: 'access content'



namespace Drupal\my_module\Controller;

use Drupal\Core\Controller\ControllerBase;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\ConnectException;

 * for created routes publication/{}
class MyModuleController extends ControllerBase {

   * {@inheritdoc}
  public function publicationDetail($halId_s) {

    $module_global_config = \Drupal::configFactory()
    $amu_hal_url_ws = $module_global_config->get('amu_hal_url_ws');
    $docType_s = $module_global_config->get('docType_s');

    $url = $amu_hal_url_ws . 'search/?fl=title_s,en_title_s,docid,label_s,en_label_s,docType_s,authIdHal_s,halId_s,structId_i,uri_s,keyword_s,en_keyword_s,authLastNameFirstName_s,journalTitle_s,abstract_s,en_abstract_s,producedDate_tdate,producedDateY_i,language_s,fileMain_s&fq=halId_s:' . $halId_s;

    try {
      $client = new Client();
      $response = $client->request('GET', $url);
      $content = json_decode($response->getBody(), TRUE);       
    } catch (ConnectException $e) {
      \Drupal::logger('amu_hal')->error('cette url n est pas bonne' . $url);

    $theme = 'page-PublicationDetails';

    $module_path = file_create_url(drupal_get_path('module', 'my_module'));

    $build = [
      '#theme' => $theme,
      "#content" => $content,
      '#url' => $url,
      '#docTypes_array' => $docType_s,
      '#module_path' => $module_path,
    return $build;



{% for doc in content.response.docs %}    
      {{ doc.title_s|first }}
    // some more //    
 {% endfor %}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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