How do I programmatically create a route? I'm trying to create a route programmatically using this code.

example.routing.yml

route_callbacks:
    - '\Drupal\example\Routing\ExampleRoutes::routes'


Drupal\example\Routing\ExampleRoutes.php



namespace Drupal\example\Routing;

use Symfony\Component\Routing\Route;

class ExampleRoutes {
    public function routes() {
        $routes = array();
        $routes['example.settings'] = new Route(
            '/example/',
            array(
              '_form' =&gt; '\Drupal\example\Form\ExampleForm',
            ),
            array(
              '_permission'  =&gt; 'access content',
            )
        );

        return $routes;
    }

}


When I install the module, I get the following error


  Class \Drupal\example\Routing\ExampleRoutes does not exist


Why doesn't the above code work?
I think that in your case the problem is in this line:

    '/example/',


that should be:

    '/example',


so without the final /.

You can try with the other method extending from RouteSubscriberBase and your routes method needs to receive as a parameter a RouteCollection.

use Drupal\Core\Routing\RouteSubscriberBase;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;

class ExampleRoutes extends RouteSubscriberBase {

  public function routes(RouteCollection $collection) {

  }

}


And you need to define the Route Subscriber as a service

  example.route_subscriber:
    class: Drupal\example\Routing\ExampleRoutes
    tags:     - { name: event_subscriber }


Take a look at this example: 

Dynamic Routes in Drupal 8 with a RouteSubscriber