Drupal 8 restresource: 找不到路由

4

我正在尝试创建一个rest端点。我的做法是首先使用drupal generate:module创建一个模块,然后添加rest依赖项,最后使用drupal generate:plugin:rest:resource创建一个rest资源。这一切都很顺利,我创建的端点甚至在模块restui中显示,路径为:/api/v1/articles。 问题是当我访问我创建的端点时,我会收到以下错误:

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

重建缓存对我来说并没有解决问题,当我查看路由表时,确实没有为/api/v1/articles添加路由。

我是否忘记了做什么?

这是生成的REST资源:

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!");
  }

}
3个回答

4
根据 REST和应用程序集成
引用:

您发送的数据的内容类型,或者您接收的数据的接受类型,必须设置为“application/hal+json”。

因此需要执行以下步骤:
  1. 启用HAL模块。
  2. 在资源配置中,在“接受的请求格式”下启用“hal_json”。
  3. 告知Drupal您正在使用的序列化格式。因此,要么发送带有hal+json的Content-Type标头(例如POST),要么将?_format=hal+json添加到URL中(例如GET)。
这也可能是由于缺少@RestResource注释中的此行而导致的,但您已经拥有它了:
"https://www.drupal.org/link-relations/create" = "/api/v1/articles"

0
问题在于restui不会为您的资源创建配置文件,如果您只想使用稳定版本(8.x-1.12),则可以自己创建。restui的dev版本已经修复了这个问题。
链接:https://www.drupal.org/node/2816433

0

所以问题出在从8.1.9升级到8.2.0。这破坏了我用来分配端点的RestUI模块。


那你是怎么解决的?降级了吗? - Jens Wegar
目前,No restui有一个修复问题的补丁。我猜他们很快就会发布它。 - Nylsoo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接