1

what are the real differences between these two methods of creating a webservice :

  1. Routing + Controller (https://www.chapterthree.com/blog/custom-restful-api-drupal-8)
  2. REST plugin (https://www.drupal.org/docs/8/api/restful-web-services-api/custom-rest-resources)

advantages/disadvantages of both?

I think we can create a restfull API with one as with the other, isn't it?

Thanks

2

actually to create a wbesrvice endpoitns in Drupal 8 we need to activate one of or both of this 2 modules :

  1. JSON:API module (best one):

after the activation of this module you can access to all Drupal entities (user , node , taxonomy ) throw a dynamic endpoints that have this structure http://{host}/json_api/{entity_type}/{bundle}?Parameters, with the ability to fitter the result + built in pagination + sorting ex of endpoints :

  • /jsonapi/node/article : get a json contains all node article.

  • /jsonapi/node/article,sort=-created : get a json contains all node article and order them with e created field in DESC order.article.

  • /jsonapi/taxojomy_term/article : get a json contains all taxnomy term category.

understand the Drupal entity type first will be better : https://www.youtube.com/watch?v=FrSjwe8bh7I

then use the JSON:api using this videos : https://www.youtube.com/watch?v=--ZL3EAhnwc&list=PLZOQ_ZMpYrZsyO-3IstImK1okrpfAjuMZ

  1. RESTful Web Services

    • Exposing Views using the RESTful Web Services module( Do you need an easy configuration from backoffice to set up and endpoint ?) https://www.youtube.com/watch?v=TMvNwRVJSEQ.
    • Creating a Custom REST Resources to expose data that can’t be exposed by the previous solution - when to use this solution when none of the previous solution can expose this data (Does JSON:API and View exposing can't expose this type of data ?)

note : the methos in this url https://www.chapterthree.com/blog/custom-restful-api-drupal-8 never use it because we get out of the context of drupal, and you waon't be able to use a hiddne power of Rest module ex :l by default any custom rest endpoint will display i if rest ui (https://www.drupal.org/project/restui) module installed .

| improve this answer | |
  • Thank you for your answer. Yes, I already know the existence of jsonapi for entities ^^. In fact, I never liked the default renderings like jsonapi nor views :p I always prefer to develop in custom :/ (it probably has disadvantages). But, so what I retained, the advantage of the REST Plugin is that we could benefit from restui quite simply. – Hasina Njaratin May 24 at 12:53
  • also the custom rest is a Drupal plugin , and Drupal plugin is just a solution for a recursive problem (design pattern) in this case problem : exposing an endpoint, so you gonna find a lot of methods inherited from the ResourceBase class in this case , that help you to solve problems that occurs a lot in Drupal , for exmaple you can easily set the authentication by installing a third module ex: drupal.org/project/simple_oauth ( aid mobarak :) ). – Marwen Amri May 24 at 13:13
  • unfortunately I never had the opportunity to call on RessourceBase (I don't even know its usefulness nor its existence :( ) with Custom Rest so I hope to delve more on the design pattern and plugin of drupal ;) Allah i barek fik – Hasina Njaratin May 24 at 13:23
2

Basically you can implement API REST using both methods, (Routing+Controller) or (Plugins).

(Routing+Controller)

  • Its an initial approach and it is not recommended to use it at these times for API REST.

(Plugins), Its recommended to use it, why?

  • Define a standard (for all developers) for creating Web services/endpoint in Drupal 8+.

  • Its in core to this purpose! Its like as a superior version of the classic (Routing+Controller) implementation with much others REST improvements.

  • It has integration with other modules and features like basic_auth, jsonapi, restui, etc (not the other method, Routing+Controller).

Among other things, basically I think is for that.

In other words, to not reinvent the wheel, use Plugins.

| improve this answer | |
  • Precisely, why look far if the classic method could answer all these points. - standard method for all developers (because now there are developers following the other and others the other) - we can adapt the construction of the API according to RESTFull. Manage methods, header and rendering format etc ... - I agree for the restui ^^ In short, REST Plugin is the new fashionable method: p to create APIs. Better to use it to be up to date ;) Thank you for your reply – Hasina Njaratin May 24 at 13:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.