The simplest way to create a custom page in drupal is with a controller. You can use drupal console to create a controller and inject a service into it. That service can be used to get information. We will cover services more in depth in the next exercise, where we will create a custom one.
The first thing we will need to do is create a custom module as a starting point. Let's use Drupal Console to do some scaffolding for us:
-
Navigate to your drupal root on the command line and type
lando drupal generate:module
This may look a little confusing, but
drupal
is just the name of the executable for drupal console. This command will start up an interactive prompt -- enter the following (an empty prompt signifies that you can hit enter to confirm the default value)Enter the new module name: > Example Enter the module machine name [example]: > Enter the module Path [modules/custom]: > Enter module description [My Awesome Module]: > Enter package name [Custom]: > Enter Drupal Core version [8.x]: > Do you want to generate a .module file? (yes/no) [yes]: > Define module as feature (yes/no) [no]: > Do you want to add a composer.json file to your module? (yes/no) [yes]: > Would you like to add module dependencies? (yes/no) [no]: > Do you want to generate a unit test class? (yes/no) [yes]: > Do you want to generate a themeable template? (yes/no) [yes]: > Do you want proceed with the operation? (yes/no) [yes]: >
You should now have the required module files:
example.info.yml
andexample.module
under/modules/custom/example
. -
Next let's use drupal console to generate a controller. Follow the example below, but make sure to use
lando drupal
instead ofvendor/bin/drupal
- Edit
web/modules/custom/example/src/Controller/ExampleController.php
changegetSiteName
to look like the following:
/**
* Getsitename.
*
* @return string|array
* Return Hello string.
*/
public function getSiteName() {
$site_name = $this->config('system.site')->get('name');
return [
'#type' => 'markup',
'#markup' => $this->t('The site name is: :site_name', [':site_name' => $site_name]),
];
}
- Make note of the
example.routing.yml
file. This file tells drupal that there is a page that can be loaded at theexample/site-name
path and how to load it.
example.example_controller_getSiteName:
path: '/example/site-name'
defaults:
_controller: '\Drupal\example\Controller\ExampleController::getSiteName'
_title: 'Get site Name'
requirements:
_permission: 'access content'
- Enable your module using the following command:
lando drush en example -y
- Visit your local development site and navigate to the path defined in your routing file (/example/site-name).
generate:controller
parameters:
// Welcome to the Drupal Controller generator
Enter the module name [module_filter]:
> example
Enter the Controller class name [DefaultController]:
> ExampleController
Enter the Controller method title (to stop adding more methods, leave this empty) []:
> Gett site Name
Enter the action method name [hello]:
> getSiteName
Enter the route path [/example/getSiteName]:
> /example/site-name
Enter the Controller method title (to stop adding more methods, leave this empty) []:
>
Do you want to generate a unit test class? (yes/no) [yes]:
>
Do you want to load services from the container? (yes/no) [no]:
> yes
Type the service name or use keyup or keydown.
This is optional, press enter to continue
Enter your service []:
> config.factory
Enter your service []:
>
Do you want proceed with the operation? (yes/no) [yes]:
> yes