1

This question already has an answer here:

I am following this tutorial: Altering existing routes and adding new routes based on dynamic ones for changing the route from '/example' to '/example/helloworld'.

I tried following code in my example module. With '/example' it is working fine, but with '/example/helloworld' which is the new altered route it is showing page not found. Am I missing something in the following code? Thanks

In file: example.routing.yml

example.content:
  path: '/example'
  defaults:
    _controller: '\Drupal\example\Controller\ExampleController::content'
    _title: 'Hello World'
  requirements:
    _permission: 'access content'

In file: example.services.yml

services:
  example.route_subscriber:
    class: Drupal\example\Routing\RouteSubscriber

In file: RouteSubscriber.php

class RouteSubscriber extends RouteSubscriberBase {

  public function alterRoutes(RouteCollection $collection){
    //Change path '/example' to '/example/helloworld'.
    if ($route = $collection->get('/example')){
      $route->setPath('/example/helloworld');
        }   
  } 
}

File structure in module,

enter image description here

marked as duplicate by kiamlaluno Dec 14 '16 at 6:23

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 2
    This question has already been put up before. drupal.stackexchange.com/questions/81362/… Additionally you have to register a service with the tag 'event_subscriber' for this class. – Ashish Deynap Dec 14 '16 at 6:17
  • 2
    Also, the argument for $collection->get() is not the path, but the route name, something like search.view. – kiamlaluno Dec 14 '16 at 6:26
  • Thank to both for pointing out. Both point were incorporated/corrected and it is done. thanks. – CodeNext Dec 14 '16 at 8:30

Browse other questions tagged or ask your own question.