Skip to main content
Drupal 8

Main navigation

  • Home
    • Question And Answer
    • NEWS
    • Projects
  • Tutorials - Drupal
    • Android
    • Other
User account menu
  • Log in

Breadcrumb

  1. Home

Create a simple module for Drupal 8 - Step 2 - Create Simple Page

By editor , 14 November, 2015
Question

How to create a simple page programmatically with Drupal 8

1. Create a info.yml file
Here : test.info.yml

name: 'Test Module'
description: 'Test module for Drupal8.ovh'
type: module
core: 8.x
varsion: 0.1-dev
package: Test

   

 

2. Create a .routing.yml
Here : test.routing.yml

test.my_page:
  path: '/mypage'
  defaults:
    _controller: '\Drupal\test\Controller\TestController::home'
    _title: 'My First Drupal 8 Page'
  requirements:
    _permission: 'access content'

 

3. Then create the Controller and put into module_root_folder/src/Controller/
Here : test/src/Controller/

<?php

/**
 * @file
 * @author Your Name
 * Contains \Drupal\test\Controller\TestController.
 */

namespace Drupal\test\Controller;

/**
 * Provides route responses for the Test module.
 */
class TestController {

    /**
     * Returns a simple page.
     *
     * @return array
     *   A simple renderable array.
     */
    public function home() {
        $element = array(
            '#markup' => 'Hello. This is my First Page',
        );
        return $element;
    }

}

4. Now you can visit your programmatically created page from your.domain/mypage

More about custom access control : https://atendesigngroup.com/blog/restricting-access-drupal-8-controllers

Tips: Set page title

$request = \Drupal::request();
if ($route = $request->attributes->get(\Symfony\Cmf\Component\Routing\RouteObjectInterface::ROUTE_OBJECT)) {
    $route->setDefault('_title', "Access denied");
}

File
test.zip (1.49 KB)

Tags

  • Drupal 8
  • Module
Create a simple module for Drupal 8 - Step 1
Create a simple module for Drupal 8 - Step 3 - Create Dynamic Page/ Block
Create a Form with Drupal 8 using Form API
How to create a JSON web service on drupal 8 ?
Insert a view on a custom page or block
Add a Form in to a Basic Controller

Comments

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.

Links

  • Drupal.org
  • Drupal core release cycle
  • Drupal events with ease
  • Unit test with Drupal using PHPUnit (Drupal 10)
  • Tokens for file upload url
  • Tips for Docker
  • CKEditor inline image files usage for custom forms.
  • Install Docker on Linux Mint
  • Install PHP 8.1
  • Drupal Security Best Practices
  • Order and sort items with tabledrag
  • Add a custom service YAML container
  • Apply a patch with composer