You are here

class RouteSubscriber in Examples for Developers 8

Same name and namespace in other branches
  1. 3.x modules/menu_example/src/Routing/RouteSubscriber.php \Drupal\menu_example\Routing\RouteSubscriber

Listens to the dynamic route events.

The \Drupal\Core\Routing\RouteSubscriberBase class contains an event listener that listens to this event. We alter existing routes by implementing the alterRoutes(RouteCollection $collection) method of this class.

Hierarchy

Expanded class hierarchy of RouteSubscriber

See also

https://www.drupal.org/docs/8/api/routing-system/altering-existing-route...

1 string reference to 'RouteSubscriber'
menu_example.services.yml in menu_example/menu_example.services.yml
menu_example/menu_example.services.yml
1 service uses RouteSubscriber
menu_example.route_subscriber in menu_example/menu_example.services.yml
Drupal\menu_example\Routing\RouteSubscriber

File

menu_example/src/Routing/RouteSubscriber.php, line 18

Namespace

Drupal\menu_example\Routing
View source
class RouteSubscriber extends RouteSubscriberBase {

  /**
   * {@inheritdoc}
   */
  public function alterRoutes(RouteCollection $collection) {

    // Get the path from RouteCollection.
    $route = $collection
      ->get('example.menu_example.path_override');

    // Set the new path.
    $route
      ->setPath('/examples/menu-example/menu-altered-path');

    // Change title to indicate changes.
    $route
      ->setDefault('_title', 'Menu item altered by RouteSubscriber::alterRoutes');
  }

}

Members

Namesort descending Modifiers Type Description Overrides
RouteSubscriber::alterRoutes public function Alters existing routes for a specific collection. Overrides RouteSubscriberBase::alterRoutes
RouteSubscriberBase::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to. 5
RouteSubscriberBase::onAlterRoutes public function Delegates the route altering to self::alterRoutes(). 1