How do I set up a Drupal 8 Module to be an AJAX service?
File structures seem a lot different in Drupal 8, and I’m still getting the hang of them. This is an example of a simple module I made, so hopefully it can be a good starting point for you too!
my_module/my_module.info.yml
my_module/my_module.module
**Drupal 8 includes a way to make URLs via a routing.yml file.
my_module/my_module.routing.yml
my_module/src/Controller/DefaultController.php
So in this example: now if you go to http://mywebsite.com/my_module/hello_world if will call the defaultcontroller in your module and print out JSON you can use in an ajax call. You’ll also notice I can use it on the server side as well (my_module.module)
To me this was a lot easier or a process than Drupal 7 was. Happy Drupal Coding!