2016-10-11 6 views
1

Ich versuche einen Rest-Endpunkt zu erstellen. Die Art, wie ich es mache, ist, dass ich zuerst ein Modul mit drupal generate:module erzeuge, dann die Restabhängigkeit hinzufüge und dann eine Restresource mit drupal generate:plugin:rest:resource erstelle. Das alles funktioniert gut, der Endpunkt, den ich erstellt habe, erscheint sogar im Modul restui mit folgendem Pfad: /api/v1/articles. Das Problem ist, wenn ich an den Endpunkt gehen i erstellt ich die folgende Fehlermeldung erhalten:Drupal 8 restresource: Keine Route gefunden

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

Cache Wiederaufbau das Problem für mich nicht beheben und wenn ich in der Routing-Tabelle sehen gibt es in der Tat kein Routing hinzugefügt für /api/v1/articles. Gibt es etwas, das ich vergesse?

Dies ist das erzeugte Rest Ressource:

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

} 

Antwort

0

So ist die Probleme waren mit einem Upgrade von 8.1.9 bis 8.2.0. Dies hat das RestUI-Modul zerstört, mit dem ich die Endpunkte zugewiesen habe.

+0

so wie haben Sie dieses Problem beheben? downgraden? –

+0

Kein Restui hat einen Patch, der das Problem vorerst behebt. Bald werden sie es veröffentlichen, denke ich. – Nylsoo

0

Das Problem ist, dass Restui keine Konfigurationsdatei für Ihre Ressource erstellt, Sie können sie selbst erstellen, wenn Sie nur mit stabilen Versionen (8.x-1.12) arbeiten möchten. Die dev-Version von restui behebt dies bereits.

-Link: https://www.drupal.org/node/2816433

1

Per 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'.

So sind die folgenden Schritte erforderlich:

  1. des HAL-Modul aktivieren.
  2. In der Ressourcenkonfiguration unter Akzeptierte Anfrageformate aktivieren hal_json.
  3. [I]nform Drupal about the serialization format you are using. Senden Sie entweder den Content-Type-Header mit hal + json (z. B. POST) oder fügen Sie ?_format=hal+json zur URL hinzu (z. B. GET).

Dies kann auch durch fehlende diese Zeile verursacht werden, aber Sie es bereits haben:

"https://www.drupal.org/link-relations/create" = "/api/v1/articles"