Có một số cảnh báo về việc truy cập trực tiếp vào đối tượng yêu cầu theo cách này trong \Drupal::request:
* Note: The use of this wrapper in particular is especially discouraged. Most
* code should not need to access the request directly. Doing so means it
* will only function when handling an HTTP request, and will require special
* modification or wrapping when run from a command line tool, from certain
* queue processors, or from automated tests.
*
* If code must access the request, it is considerably better to register
* an object with the Service Container and give it a setRequest() method
* that is configured to run when the service is created. That way, the
* correct request object can always be provided by the container and the
* service can still be unit tested.
Bất kỳ bộ điều khiển biểu mẫu nào \Drupal\Core\Form\FormBasetự động mở rộng đều có phần phụ thuộc này được chèn và nó có thể được truy cập bằng cách sử dụng:
$this->getRequest()->getSchemeAndHttpHost()
Tôi nghĩ (nhưng chưa được kiểm tra) rằng một bộ điều khiển trang thông thường mở rộng \Drupal\Core\Controller\ControllerBasecó thể cung cấp request_stackdịch vụ bằng cách ghi đè \Drupal\Core\Controller\ControllerBase::createchức năng và sau đó đặt $requestthuộc tính trong hàm tạo. Điều này được mô tả thực sự tốt cho các biểu mẫu và quy trình tương tự sẽ được áp dụng cho các bộ điều khiển trang: https://www.drupal.org/docs/8/api/service-and-dependency-injection/dependency-injection-for-a- hình thức .