How can I have an URI with two GET? I wrote my custom rest endpoint. I need to 2 URI both called with GET


/api/item/{id} to get single item identified by ID
/api/item to get the list of items.


i try with

  @RestResource(
    id = "my_id",
    label = @Translation("My label"),
    uri_paths = {
      "canonical" = "/api/item",
      "https://www.drupal.org/link-relations/index" = "/api/item/{id}",
    }


into my class i have

class ApiRestResource extends ResourceBase {

    public function get($id = NULL) {


but only canonical URI is found. the message for the second URI is

"message": "No route found for \"GET /api/item/1\""


how can i have 2 GET in my class?
thanks in advance
you can create two resource files, one that receives the parameter and another that does not.

Example:


  demo_rest_api.info.yml.


name: Demo REST API
description: Define's a custom REST Resource
package: Custom
type: module
core: 8.x



  /src/Plugin/rest/resource/DemoResource.php


&lt;?php

namespace Drupal\demo_rest_api\Plugin\rest\resource;

use Drupal\rest\Plugin\ResourceBase;
use Drupal\rest\ResourceResponse;

/**
 * Provides a Demo Resource
 *
 * @RestResource(
 *   id = "demo_resource",
 *   label = @Translation("Demo Resource"),
 *   uri_paths = {
 *     "canonical" = "/api/item"
 *   }
 * )
 */
class DemoResource extends ResourceBase {

  /**
   * Responds to entity GET requests.
   * @return \Drupal\rest\ResourceResponse
   */
  public function get() {


      $response = ['message' =&gt; 'Hello, this is a rest service:'];

    return new ResourceResponse($response);

  }
}



  /src/Plugin/rest/resource/DemoResource_item_id.php


&lt;?php

namespace Drupal\demo_rest_api\Plugin\rest\resource;

use Drupal\rest\Plugin\ResourceBase;
use Drupal\rest\ResourceResponse;

/**
 * Provides a Demo Resource
 *
 * @RestResource(
 *   id = "DemoResource_item_id",
 *   label = @Translation("Demo Resource item id"),
 *   uri_paths = {
 *     "canonical" = "/api/item/{id}"
 *   }
 * )
 */
class DemoResource_item_id extends ResourceBase {

  /**
   * Responds to entity GET requests.
   * @return \Drupal\rest\ResourceResponse
   */
  public function get($id= null) {


      $response = ['message' =&gt; 'Hello, this is a rest service and parameter is: '.$id];

    return new ResourceResponse($response);

  }
}