• Drupal8 新建第一个模块


    参考:

    https://www.drupal.org/developing/modules/8

    https://www.drupal.org/node/1915030

    https://www.drupal.org/node/318

    Drupal 8 RC 发布了。由于D8使用了Symfony2的内核。与之前版本对比,模块的写法有很大的不同。

    新建模块

    下面的例子中写一个单独显示“hello world”页面的模块。

    首先要给模块起一个唯一的机器名(machine name)

    机器名必须满足:

    1. 字母开头

    2. 只能包含小写字母和下划线

    3. 必须唯一,不能与其他模块,主题和配置文件(profile)重名

    4. 不能是src, lib, vendor, assets, css, files, images, js, misc, templates, includes, fixtures, Drupal. 这些是保留关键字。

    info.yml

    在根目录的modules目录中先新建custom和contrib目录。

    custom存放我们自己的写的模块,contrib里放从官网下的模块。当然不建这俩目录系统也能识别不过这是规范。然后我们在custom目录下新建"hello_world"目录。

    然后新建hello_world.info.yml,在D7是.info文件。此文件记录模块的元信息。就是最基本的信息。

    name 模块的名字,description 模块的描述干什么用的。package 属于哪个分组。如Core属于D8内核模块。

    type可以为module,theme或profile。

    name: Hello World Module
    description: Creates a page showing "Hello World".
    package: Custom
    type: module
    version: 1.0
    core: 8.x

    composer.json 非必须

    新建composer.json

    {
        "name": "drupal/example",
        "description": "This is an example composer.json for example module.",
        "type": "drupal-module",
        "license": "GPL-2.0+"
    }

    添加控制器

    在hello_world目录中新建/src/Controller/HelloController.php

    <?php
    /**
     * @file
     * Contains Drupalhello_worldControllerHelloController.
     */
    
    namespace Drupalhello_worldController;
    
    use DrupalCoreControllerControllerBase;
    
    class HelloController extends ControllerBase {
      public function content() {
        return array(
            '#type' => 'markup',
            '#markup' => t('Hello, World!'),
        );
      }
    }
    ?>

    返回这个模块目录下新建hello_world.routing.yml

    hello_world.content:
      path: '/hello'
      defaults:
        _controller: 'Drupalhello_worldControllerHelloController::content'
        _title: 'Hello World'
      requirements:
        _permission: 'access content'

    到此,这个模块算完整的写完,这时候打开地址/hello就会出现hello world的单独页面。

  • 相关阅读:
    Java之泛型练习
    集合框架-Map练习-记录字母出现的次数
    集合框架Map之entrySet方法的使用
    集合框架Map之KeySet方法的使用
    Java集合框架之LinkedList-----用LinkedList模拟队列和堆栈
    springboot2.0+mycat实验读写分离
    mysql主从复制
    mycat读写分离
    kafka初探
    redis-List类型
  • 原文地址:https://www.cnblogs.com/mafeifan/p/4867914.html
Copyright © 2020-2023  润新知