How do I pass JavaScript variables back to PHP? I'm trying to build a Drupal 8 module with a jQuery plugin. I wanted to pass some of the JavaScript variables to PHP so that I can save the variables into database. 

This question asked about the way to do it in Drupal 7. Since hook_menu() is gone in Drupal 8, how can I accomplish this task?
Where you implemented hook_menu(), in Drupal 8 you need:


A .routing.yml file containing the routing definition

example.name:
  path: '/example/{variable}'
  defaults:
    _controller: '\Drupal\example\Controller\ExampleController::content'
  requirements:
    _permission: 'access content'

A controller class for the page callback

class ExampleController {  
  // ...
  public function content($variable) {
    // Do something with $variable.
  }
}



What is important is the file structure, which should be as follows, assuming that example is the machine name of the module.


example
The directory containing the module


example.routing.yml
src
The directory containing all the classes


Controller (directory)


ExampleController.php
The file containing the controller





Keep in mind that you can change the machine name of the module, the route name (example.name), and the name of the class implementing the controller (ExampleController); the ones I used are only examples.