How do I pass JavaScript variables back to PHP?
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.