2

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' => '\Drupal\example\Form\ExampleForm',
            ),
            array(
              '_permission'  => '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?

  • Is the folder structure of the module correct? – Kevin Jul 20 '18 at 14:30
  • 1
    Hi Kevin, yes as far as I can see. As you can see above, the paths are correct – Sergiu Tripon Jul 20 '18 at 14:31
  • 1
    The file should live at “mymodule/src/Routing/“ – Kevin Jul 20 '18 at 14:32
  • 1
    Yes, that's correct – Sergiu Tripon Jul 20 '18 at 14:32
  • 1
    Why are you attempting to create it like this indead of using .routing.yml file? – No Sssweat Jul 20 '18 at 23:56
1

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

  • The question is asking why the code causes that error. It's correct to use a route callback, since it is also used from Drupal core modules. – kiamlaluno Oct 28 '18 at 12:45
  • @kiamlaluno I know my answer first part solve the problem and I added anohter way after. – Adrian Cid Almaguer Oct 28 '18 at 19:23
  • I didn't mean it was wrong, but that should make clearer the approach shown in the question works too, once fixed the behind error. The error message shown in the question appears before the new route is added. The trailing slash in a string doesn't avoid Drupal finds the class, since to find the string Drupal should first find the class, but it's the class that Drupal doesn't find. – kiamlaluno Oct 28 '18 at 20:39
0

Try the following example it works for me.

example.routing.yml

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

src/Routing/ExampleRoutes.php

namespace Drupal\example\Routing;

use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;

class ExampleRoutes{

  public function routes(){
    $route_collection = new RouteCollection();
    $route            = new Route(
    // Path to attach this route to:
      'example',
      // Route defaults:
      [
        '_form' => 'Drupal\example\Form\ExampleForm',
      ],
      // Route requirements:
      [
        '_permission' => 'access content',
      ]
    );
    $route_collection->add('example.settings', $route);
    return $route_collection;
  }

}

Note that path param 'example' shouldn't have any '/' .

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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