Joomla 简明教程

Joomla - Create Modules

在本章中,我们将学习 Joomla 中的 Creating Modules 。模块是灵活且轻量级的扩展,可用于页面渲染。

Create Modules

以下是使用 Joomla 创建模块的简单步骤。

Step 1 − 在您的 Joomlamodules 文件夹中创建一个名为 mod_firstmodule 的文件夹。

joomla createmodules step1

Step 2 − 在 mod_firstmodule 文件夹中创建一个名为 "helper.php" 的文件。此文件包含名称为 helper 的类,它有助于在模块输出中显示检索到的数据。

helper.php

<?php
   /**
      * Helper class for Hello World! module
      *
      * @package    Joomla.Tutorials
      * @subpackage Modules
      * @link http://docs.joomla.org/J3.x:Creating_a_simple_module/Developing_a_Basic_Module
      * @license        GNU/GPL, see LICENSE.php
      * mod_helloworld is free software. This version may have been modified pursuant
      * to the GNU General Public License, and as distributed it includes or
      * is derivative of works licensed under the GNU General Public License or
      * other free or open source software licenses.
   */

   class ModHelloWorldHelper {
      /**
         * Retrieves the hello message
         *
         * @param   array  $params An object containing the module parameters
         *
        * @access public
      */

      public static function getHello($params) {
         return 'Hello, World!';
      }
   }

?>

Step 3 − 创建一个名为 mod_helloworld.php 的文件。它是模块的入口点,它执行初始化例程,收集必要的数据并使用模板显示模块输出。

mod_helloworld.php

<?php
   /**
      * Hello World! Module Entry Point
      *
      * @package    Joomla.Tutorials
      * @subpackage Modules
      * @license    GNU/GPL, see LICENSE.php
      * @link       http://docs.joomla.org/J3.x:Creating_a_simple_module/Developing_a_Basic_Module
      * mod_helloworld is free software. This version may have been modified pursuant
      * to the GNU General Public License, and as distributed it includes or
      * is derivative of works licensed under the GNU General Public License or
      * other free or open source software licenses.
   */

   // No direct access
   defined('_JEXEC') or die;

   // Include the syndicate functions only once
   require_once dirname(__FILE__) . '/helper.php';

   $hello = modHelloWorldHelper::getHello($params);
   require JModuleHelper::getLayoutPath('mod_helloworld');
?>

Step 4 − 创建一个 mod_helloworld.xml file 。此文件包含有关模块的信息。此 xml 文件包含有关为模块安装到 Joomla 中的文件的信息。

mod_helloworld.xml file

<?xml version = "1.0" encoding = "utf-8"?>

<extension type = "module" version = "3.1.0" client = "site" method="upgrade">

   <name>Hello, World!</name>
   <author>Tutorials Point</author>
   <version>1.0.0</version>
   <description>A simple Hello World! module.</description>

   <files>
      <filename>mod_helloworld.xml</filename>
      <filename module = "mod_helloworld">mod_helloworld.php</filename>
      <filename>index.html</filename>
      <filename>helper.php</filename>
      <filename>tmpl/default.php</filename>
      <filename>tmpl/index.html</filename>
   </files>

   <config>
   </config>

</extension>

Step 5 − 创建一个名为 index.html 的简单 html 文件。编写此文件的目的是,不应浏览所创建的目录。当用户浏览这些目录时,将显示 index.html 文件。您甚至可以保持此文件为空。

index.html

<html>
   <body> Welcome to Tutorials Point!!!!! </body>
</html>

Step 6 − 创建一个名为 tmpl 的文件夹。如下所示将 default.php 文件和 index.html(在步骤 (5) 中创建)放在 tmpl 文件夹下。default.php 文件是一个模板,用于显示模块输出。

default.php

<?php
   /**
      * @package Joomla.Site
      * @subpackage mod_firstmodule
      * @copyright Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
      * @license GNU General Public License version 2 or later; see LICENSE.txt
   */
defined('_JEXEC') or die;
>

<p>Hello World!!!!!!</p>

在创建完所有这些文件后,压缩整个文件夹 mod_firstmodule

Step 7 −转到Joomla管理员中的 ExtensionExtension Manager ,您将获得以下屏幕。您可以在此处上传并安装您创建的模块文件,即 mod_firstmodule 文件夹。单击 Choose File 并选择创建的模块文件(压缩文件)。单击 Upload & Install 按钮以上传模块文件。

joomla createmodules step7

Step 8 −上传和安装后,转到 Module Manager 并单击 New 。在那里,您可查看创建的模块文件,如下所示。

joomla createmodules step8

Step 9 −您可以像其他模块一样分配此模块,然后发布它。