n How to create a custom page programmatically in Drupal 8 | CodimTh

Please Disable Your Browser Adblock Extension for our site and Refresh This Page!

our ads are user friendly, we do not serve popup ads. We serve responsible ads!

Refresh Page
Skip to main content
On . By CodimTh
Category:

Create a custom page with custom controller at /hello that displays a title of ‘Create a custom page in drupal 8!’ and the following content: ‘Welcome to codimth.com !’

Add the following code to the codimth_controller.info.yml file:

name: CodimTh Controller
description: Create a custom page in drupal 8
package: CodimTh
type: module
core: 8.x

Add the following code to the codimth_controller.routing.yml file:

codimth.content:
  path: '/hello'
  defaults:
    _controller: '\Drupal\codimth_controller\Controller\CodimthController::index'
    _title: 'Create a custom page in drupal 8!'
  requirements:
    _permission: 'access content'

Add the following code to the codimth_controller/src/Controller/CodimthController.php file:

<?php
namespace Drupal\codimth_controller\Controller;
use Drupal\Core\Controller\ControllerBase;

class CodimthController extends ControllerBase
{
    public function index()
    {
        return array(
            '#type' => 'markup',
            '#markup' => $this->t('Welcome to codimth.com !'),);
    }
}

 

Now clear your Drupal 8 caches. and you should be able to see the new page.

 

Drupal 8 Create a custom page

I hope you found this article useful. let me know if you have any questions and I’ll be happy to answer them. 

Riadh Rahmi

Senior Web Developer PHP/Drupal & Laravel

I am a senior web developer, I have experience in planning and developing large scale dynamic web solutions especially in Drupal & Laravel.

Web Posts

Search

Page Facebook