2

A custom module called TSA contains almost all of the functionality for the /tsa subsite on my Drupal 8 multisite.

I want to define a route for http://localhost/tsa/, so that I can serve up dynamic content on the homepage depending on who the authenticated user is.

Here's tsa.routing.yml:

tsa.homepage:
  path: '/'
  defaults:
    _title: 'TSA Home'
    _controller: '\Drupal\tsa\Controller\HomepageController::content'
  requirements:
    _permission: 'access content'

And here's src/Controller/HomepageController.php:

<?php

namespace Drupal\tsa\Controller;
use Drupal\Core\Controller\ControllerBase;

class HomepageController extends ControllerBase {
  public function content() {
    return [
      '#type' => 'markup',
      '#markup' => 'Hello World from the HomepageController',
    ];
  }
}

But when I navigate to http://localhost/tsa, I just get Page Not Found.

Why doesn't it work? How should I go about doing this?

3

This is a classic case of trying to do something in the non-Drupal way -- conceptually, defining a custom route for your home page route makes sense, and it's possible in other frameworks, but Drupal has a different way of doing things that makes this approach completely unintuitive.

Here's how to do it:

  1. Define your route as something other than '/' (i.e. '/students/overview')
  2. In /admin/config/system/site-information, set your "Default Front Page" to whatever your route was:

enter image description here

Voila!

|improve this answer|||||
0

Also looking at your code you presented above, you have a typo.

'acess content' should be 'access content'

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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