Drupal 8 获取空 $_GET 变量

Drupal 8 getting empty $_GET variable

我正在使用 Drupal 8,我正在尝试接收一个名为 id 的 get 变量,url 看起来像 http://localhost/drp/listado/editar/2

这是控制器中的函数

public function edit(Request $request){

    $id = $request->query->get('id');
    $conexion = \Drupal::database();
    $query = $conexion->query("Select * from node where nid = ".$id);
    $result = $query->fetchAll();

  return array(
      '#id' => $id,
      '#datos' => $result,
      '#theme' => 'editar',
      '#titulo' => 'Editar'
      );

  }

但我在 reports/dblogs

中收到此错误
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 1: Select * from node where nid =;

这是custom_controller.routing.yml

custom_controller.editar:
  path: '/listado/editar/{id}'
  defaults:
    _controller: '\Drupal\custom_controller\Controller\ListController::edit'
    _title: 'Edit'
  requirements:
    _permission: 'access_content'

它没有获取变量的值,欢迎任何帮助,谢谢

我认为您的控制器接收的第一个参数是 id,第二个参数是自动请求。看这里https://www.drupal.org/docs/8/api/routing-system/using-parameters-in-routes

也许您可以打印第一个参数的内容,然后die()检查 Drupal 传递的值。

首先安装开发模块并转储$request变量。点击url后可以得到id参数。