rest - Drupal 8 资源 : No route found

标签 rest drupal drupal-8

我正在尝试创建一个休息端点。我这样做的方法是首先使用 drupal generate:module 创建一个模块然后添加其余依赖项,然后使用 drupal generate:plugin:rest:resource 创建一个 restresource .这一切都很好,我创建的端点甚至显示在模块 restui 中,路径如下:/api/v1/articles .
问题是当我转到我创建的端点时,我收到以下错误:

{ 
  "message": "No route found for \"GET /api/v1/articles\""
}

缓存重建并不能解决我的问题,当我查看路由表时,确实没有为 /api/v1/articles 添加路由.
有什么我忘记做的吗?

这是生成的休息资源:
namespace Drupal\api_articles\Plugin\rest\resource;

use Drupal\Core\Session\AccountProxyInterface;
use Drupal\rest\Plugin\ResourceBase;
use Drupal\rest\ResourceResponse;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Psr\Log\LoggerInterface;

/**
 * Provides a resource to get view modes by entity and bundle.
 *
 * @RestResource(
 *   id = "article_rest_resource",
 *   label = @Translation("Article rest resource"),
 *   uri_paths = {
 *     "canonical" = "/api/v1/articles",
 *     "https://www.drupal.org/link-relations/create" = "/api/v1/articles"
 *   }
 * )
 */
class ArticleRestResource extends ResourceBase {

  /**
   * A current user instance.
   *
   * @var \Drupal\Core\Session\AccountProxyInterface
   */
  protected $currentUser;

  /**
   * Constructs a Drupal\rest\Plugin\ResourceBase object.
   *
   * @param array $configuration
   *   A configuration array containing information about the plugin instance.
   * @param string $plugin_id
   *   The plugin_id for the plugin instance.
   * @param mixed $plugin_definition
   *   The plugin implementation definition.
   * @param array $serializer_formats
   *   The available serialization formats.
   * @param \Psr\Log\LoggerInterface $logger
   *   A logger instance.
   * @param \Drupal\Core\Session\AccountProxyInterface $current_user
   *   A current user instance.
   */
  public function __construct(
    array $configuration,
    $plugin_id,
    $plugin_definition,
    array $serializer_formats,
    LoggerInterface $logger,
    AccountProxyInterface $current_user) {
    parent::__construct($configuration, $plugin_id, $plugin_definition, $serializer_formats, $logger);

    $this->currentUser = $current_user;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static(
      $configuration,
      $plugin_id,
      $plugin_definition,
      $container->getParameter('serializer.formats'),
      $container->get('logger.factory')->get('api_articles'),
      $container->get('current_user')
    );
  }

  /**
   * Responds to GET requests.
   *
   * Returns a list of bundles for specified entity.
   *
   * @throws \Symfony\Component\HttpKernel\Exception\HttpException
   *   Throws exception expected.
   */
  public function get() {

    // You must to implement the logic of your REST Resource here.
    // Use current user after pass authentication to validate access.
    if (!$this->currentUser->hasPermission('access content')) {
      throw new AccessDeniedHttpException();
    }

    return new ResourceResponse("Implement REST State GET!");
  }

}

最佳答案

根据 REST and Application Integration :

The content type for the data you send, or the accept type for the data you are receiving, must be set to 'application/hal+json'.



所以需要以下步骤:
  • 启用 HAL 模块。
  • 在资源配置中,在接受的请求格式下启用 hal_json。
  • [I]nform Drupal about the serialization format you are using .所以要么使用 hal+json(例如 POST)发送 Content-Type header ,要么添加 ?_format=hal+json到 URL(例如 GET)。

  • 这也可能是由于在 @RestResource 中缺少此行造成的。注释,但你已经有了它:
    "https://www.drupal.org/link-relations/create" = "/api/v1/articles"
    

    关于rest - Drupal 8 资源 : No route found,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39973114/

    相关文章:

    javascript - 如何进行 6 个以上并发 REST API 调用

    德鲁巴 6 : how to get data from a custom module in to a template file

    mysql - 用。。。来代替 &

    php - 应用 Composer 的 Drupal 8 补丁修复签名字段上的多个字段

    mysql - Drupal 8 没有连接到 Docker 中的 Mysql

    asp.net - 在 ASP.NET 中创建 Web API

    excel - 从 Excel 2016 中的 API 获取数据

    drupal - 以编程方式/通过 API 创建 Drupal CCK 内容

    REST API - 需要关于架构的意见

    php - 人们如何处理内容管理系统的生产阶段?