How to create a custom page programmatically in Drupal 8

1
(1)

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 itblog.webdigg.com !’

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

name: itblog controller
description: Create a custom page in drupal 8
package: webdigg
type: module
core: 8.x

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

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

Add the following code to the itblog_controller/src/Controller/ItblogController.php file:

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

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

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

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

Similar Posts:

763

How useful was this post?

Click on a star to rate it!

Average rating 1 / 5. Vote count: 1

No votes so far! Be the first to rate this post.

Scroll to Top