3

I have defined a custom dynamic route that works, but isn't ideal:

/**
* Defines dynamic routes.
*/
class GuideUnitRoutes {

  /**
   * {@inheritdoc}
   */
  public function routes() {
    $route_collection = new RouteCollection();

    $route = new Route(
      "/guide_name/{guide_id}/{child}",
      [
        '_controller' => '\Drupal\guides\Controller\GuidesUnitController::view',
        '_title' => 'Hello',
      ],
      [
        'guide_id' => '\\w+',
        'child' => '\\w+',
        '_permission' => 'access content',
      ]
    );

    $route_collection->add("guide.child", $route);

    $route_collection->setMethods(['GET']);

    return $route_collection;
  }

}

If I define it to be ideal, it stops working:

/**
* Defines dynamic routes.
*/
class GuideUnitRoutes {

  /**
   * {@inheritdoc}
   */
  public function routes() {
    $route_collection = new RouteCollection();

    $route = new Route(
      "/guide_name-{guide_id}/{child}",
      [
        '_controller' => '\Drupal\guides\Controller\GuidesUnitController::view',
        '_title' => 'Hello',
      ],
      [
        'guide_id' => '\\w+',
        'child' => '\\w+',
        '_permission' => 'access content',
      ]
    );

    $route_collection->add("guide.child", $route);

    $route_collection->setMethods(['GET']);

    return $route_collection;
  }

}

The route that works is guide_name/foo/bar while guide_name-foo/bar doesn't work. I would like to know why.

Reading the Symfony docs it looks like this should be a problem. But for some reason Drupal isn't liking this. Is there an undocumented (or maybe a document that I missed) that forces tokens to be delimitated with /. right now if I change the - to a / and reload the same route it works.

1 Answer 1

5

The dash is not the problem, you could use a dash in the fixed part, but ...

Unlike generic Symfony routes, Drupal requires that a slug occupies a complete path part - the portion between two slashes (or everything after the last slash).

https://www.drupal.org/docs/8/api/routing-system/parameters-in-routes/using-parameters-in-routes

Alternatively you can use path processing to rewrite the path to comply with the Drupal routing system. See Do new routes in Drupal 8 *require* a static first portion?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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