Drupal8 Development Tutorial: module Development--Creating a new page

Source: Internet
Author: User
Tags drupal

We have learned about the module's Yaml file by using the Drupal8 development tutorial: Understanding. info.yml file, and today we look at how to add a new page in the way of module development.

In Drupal 7, it is very simple to add a new page through a module, using the Hook_menu definition path and callback function to return the results to be displayed. However, Drupal 8 is a lot more cumbersome, although it is also defined as a path, and then associated with the function used to display the content, but in the two files in separate. And the Controller file also has directory structure requirements, these changes need to slowly adapt.

In addition to the Hello_world.info.yml file defined above, you will need to add the Hello_world.routing.yml and src/controller/helloworldcontroller.php two files to the module directory.

The Hello_world.routing.yml file is used to define the path, page title, callback function, and related permissions that need to be added, in the same way as Hook_menu () in the previous Drupal version.

The following is the contents of the Hello_world.routing.yml file, each key word is very clear, it is not much explained:

hello_world.content:  path: ‘/hello‘  defaults:    _controller: ‘\Drupal\hello_world\Controller\HelloWorldController::content‘    _title: ‘Hello World‘  requirements:    _permission: ‘access content‘

The controller used in the above code has not yet been defined, then add the helloworldcontroller.php file, and note that its full directory structure is src/controller/helloworldcontroller.php

<?php/** * @file  * Contains \Drupal\hello_world\Controller\HelloWorldController. */ namespace Drupal\hello_world\Controller;use Drupal\Core\Controller\ControllerBase;class HelloWorldController extends ControllerBase {  public function content() {    return array(      ‘#type‘ => ‘markup‘,      ‘#markup‘ => $this->t(‘Hello, World!‘)    );  }}

After completing the HELLO_WORLD.INFO.YML, hello_world.routing.yml and helloworldcontroller.php three files, go to the Module Management page, after enabling the Hello World module, You can access the newly added Hello World page by accessing the/hello path. If you have previously enabled the module, remember to clear the cache.

Original title: Drupal8 Development Tutorial: module Development--Create a new page

Original address: http://drupalct.org/drupal-development/add-new-page-with-module-development.html

Drupal8 Development Tutorial: module Development--Creating a new page

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.