You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Drupal 8中能否创建带ID的路由调用第三方API渲染模板?

在Drupal 8中创建自定义路由并拉取第三方API数据的实现方案

当然可以实现!我之前做过类似的需求,完全不需要依赖Drupal实体,纯靠自定义路由和控制器就能搞定,下面一步步给你拆解:

1. 先准备一个自定义模块

首先得有个自定义模块(Drupal 8的自定义路由必须依托模块存在),比如命名为custom_event(模块名只要符合Drupal规范就行),至少要创建custom_event.info.yml让Drupal识别它:

name: Custom Event Pages
type: module
description: 'Creates custom event pages pulling data from third-party API'
core_version_requirement: ^8 || ^9 || ^10 # 兼容Drupal8及以上版本
package: Custom

2. 定义/events/{id}路由

在模块根目录创建custom_event.routing.yml,这就是我们定义动态路由的核心文件:

custom_event.view:
  path: '/events/{id}'
  defaults:
    _controller: '\Drupal\custom_event\Controller\EventController::viewEvent'
    _title: 'Event Details'
  requirements:
    _access: 'TRUE' # 这里可以换成自定义权限,比如 `permission:access content` 限制登录用户访问
    id: '\d+' # 限制id为数字,可根据你的API规则调整正则

这里的{id}是动态参数,会自动传递给后续的控制器方法。

3. 编写控制器处理API调用与数据传递

在模块的src/Controller/目录下创建EventController.php,这是处理业务逻辑的核心:

<?php

namespace Drupal\custom_event\Controller;

use Drupal\Core\Controller\ControllerBase;
use GuzzleHttp\ClientInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Controller for event detail pages.
 */
class EventController extends ControllerBase {

  /**
   * The HTTP client service.
   *
   * @var \GuzzleHttp\ClientInterface
   */
  protected $httpClient;

  /**
   * Constructs an EventController object.
   *
   * @param \GuzzleHttp\ClientInterface $http_client
   *   The HTTP client.
   */
  public function __construct(ClientInterface $http_client) {
    $this->httpClient = $http_client;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('http_client')
    );
  }

  /**
   * Displays event data fetched from third-party API.
   *
   * @param int $id
   *   The event ID passed from the route.
   *
   * @return array
   *   A render array for the theme system.
   */
  public function viewEvent($id) {
    try {
      // 替换成你的第三方API地址
      $api_response = $this->httpClient->request('GET', "https://your-api-domain.com/events/{$id}");
      // 解析API返回的JSON数据
      $event_data = json_decode($api_response->getBody()->getContents(), TRUE);

      // 如果API返回空数据,跳转到404页面
      if (empty($event_data)) {
        throw new \Exception('Event data not found');
      }

      // 把处理好的数据传递给模板
      return [
        '#theme' => 'custom_event_view',
        '#event' => $event_data,
        '#cache' => [
          'keys' => ['custom_event', $id],
          'max-age' => 3600, // 缓存1小时,可根据数据更新频率调整
        ],
      ];
    }
    catch (\Exception $e) {
      // 捕获API请求异常,返回404页面
      return $this->redirect('system.404');
    }
  }

}

这里用了Drupal自带的Guzzle客户端(依赖注入是Drupal的标准做法,别直接实例化Guzzle哦),同时做了基础的错误处理,避免页面崩溃。

4. 定义模板渲染数据

首先在custom_event.module里实现hook_theme()注册模板:

<?php

/**
 * Implements hook_theme().
 */
function custom_event_theme() {
  return [
    'custom_event_view' => [
      'variables' => ['event' => NULL],
      'template' => 'custom-event-view',
    ],
  ];
}

然后在模块的templates/目录下创建custom-event-view.html.twig,用Twig语法渲染数据:

<div class="event-detail-card">
  <h1 class="event-title">{{ event.title }}</h1>
  <div class="event-date">
    <span class="label">Date:</span> {{ event.start_date|date('F j, Y') }}
  </div>
  <div class="event-description">
    {{ event.description|raw }}
  </div>
  <div class="event-location">
    <span class="label">Location:</span> {{ event.venue }}
  </div>
</div>

注意:模板里的字段名要和你的第三方API返回的字段完全对应。

最后收尾操作

  • 在Drupal后台启用custom_event模块
  • 清除Drupal缓存(这步很关键,不然路由和模板不会生效)
  • 访问/events/123(把123换成真实的事件ID),就能看到拉取的第三方数据了

额外小建议

  • 不要把API密钥硬编码在代码里,可通过Drupal的配置表单存储敏感信息
  • 根据数据更新频率调整缓存策略,减少不必要的API请求
  • 可以给路由添加更精细的权限控制,比如只允许特定角色访问

内容的提问来源于stack exchange,提问作者Kevin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 03:17:39