码迷,mamicode.com
首页 > 其他好文 > 详细

Drupal8 新建第一个模块

时间:2015-10-10 21:40:36      阅读:154      评论:0      收藏:0      [点我收藏+]

标签:

参考:

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目录中新建"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 \Drupal\hello_world\Controller\HelloController.
 */

namespace Drupal\hello_world\Controller;

use Drupal\Core\Controller\ControllerBase;

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: ‘\Drupal\hello_world\Controller\HelloController::content‘
    _title: ‘Hello World‘
  requirements:
    _permission: ‘access content‘

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

 

Drupal8 新建第一个模块

标签:

原文地址:http://www.cnblogs.com/mafeifan/p/4867914.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!