Open drupal installation folder
C:\wamp\www\drupal_learning\DL_08072023\drupal-10.1.1\modules
Now you create a folder with the name of “custom”
inside the custom folder create another one folder with name of custome module name.( like hello_world).
We can create one file hello_world.info.yml (with the extenion of .info.yml)
Inside the info.yml file enter below the text contents
name: Hello World Module
description: Creates a page showing "Hello World".
package: Custom
type: module
core_version_requirement: ^9.4 || ^10
And save it
Then open the extend menu search custom module name and install it.
How to Add a routing file
Back in your module's root folder, where the .info.yml file is located, add a new file called hello_world.routing.yml and add the following to it:
hello_world.content:
path: '/hello'
defaults:
_controller: '\Drupal\hello_world\Controller\HelloController::content'
_title: 'Hello World'
requirements:
_permission: ‘access content’
path: '/hello' - > route name
How to Add a controller folder
Within your module folder, you should have the PSR-4 standard folder structure
/src/Controller
and inside this folder you should have your HelloController.php controller file.
So your controller file will be like this
/src/Controller/HelloController.php
<?php
namespace Drupal\hello_world\Controller;
use Drupal\Core\Controller\ControllerBase;
/**
* Defines HelloController class.
*/
class HelloController extends ControllerBase {
/**
* Display the markup.
*
* @return array
* Return markup array.
*/
public function content() {
return [
'#type' => 'markup',
'#markup' => $this->t('Hello, World!'),
];
}
}
namespace Drupal\hello_world\Controller; -> folder name
class HelloController extends ControllerBase { ->highlighted text are caps letter
Before run we should clear chache