How to override a routing and controller in Drupal8

When using below module this is what happens

1. /user/login => page not found
2. /login1234 => shows login page (user.login route is hijacked by this modules route subscriber)
3. /login1234 => shows text "hello world" (user.login controller is hijacked by this modules route subscriber)


example_module.info.yml
name: 'example_module'
type: module
description: 'My Awesome Module'
core: 8.x
package: 'Custom'


example_module/example_module.services.yml
services:
  example_module.route_subscriber:
    class: Drupal\example_module\Routing\RouteSubscriber
    tags:
      - { name: event_subscriber }

example_module/example_module.module
<?php

/**
 * @file
 * Contains example_module.module.
 */

use Drupal\Core\Routing\RouteMatchInterface;

/**
 * Implements hook_help().
 */
function example_module_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    // Main module help for the example_module module.
    case 'help.page.example_module':
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('My Awesome Module') . '</p>';
      return $output;

    default:
  }
}

/**
 * Implements hook_theme().
 */
function example_module_theme() {
  return [
    'example_module' => [
      'render element' => 'children',
    ],
  ];
}

example_module/src/Controller/ExampleModuleController.php
<?php
/**
 * @file
 * Contains \Drupal\example_module\Controller\ExampleModuleController.
 */
// THIS FILE BELONGS AT /example_module/src/Controller/ExampleModuleController.php
namespace Drupal\example_module\Controller;
use Drupal\Core\Controller\ControllerBase;
/**
 * An example controller.
 */
class ExampleModuleController extends ControllerBase {
  /**
   * {@inheritdoc}
   */
  public function content() {
    $build = array(
      '#type' => 'markup',
      '#markup' => t('Hello World!'),
    );
    return $build;
  }
}


example_module/src/Routing/RouteSubscriber.php
<?php

namespace Drupal\example_module\Routing;

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

/**
 * Class RouteSubscriber.
 *
 * Listens to the dynamic route events.
 */
class RouteSubscriber extends RouteSubscriberBase {

  /**
   * {@inheritdoc}
   */
  protected function alterRoutes(RouteCollection $collection) {
    // Replace "some.route.name" below with the actual route you want to override.
    if ($route = $collection->get('user.login')) {
      //echo "calledh here";
      //exit;
      $route->setPath('/login123');
      //debug($route);
      //exit;
      //echo "called here";
      //exit;
       $route->setDefaults(array(
        '_controller' => '\Drupal\example_module\Controller\ExampleModuleController::content',
        ));
    }
  }

}




Comments

Popular posts from this blog

Programatically create layout builder section in Drupal

Code quality analysis of Drupal. Can I use Sonar?

Set up Drupal7 to Drupal8 migration in simple steps (using Drush)