目录

Joomla - 创建模块( Create Modules)

在本章中,我们将研究在Joomla中Creating Modules 。 模块是灵活且轻量级的扩展,可用于页面呈现。

创建模块

以下是在Joomla中创建模块的简单步骤。

Step (1) - 在Joomlamodules文件夹中创建一个名为mod_firstmodule文件夹。

Joomla创建模块

Step (2) - 在mod_firstmodule文件夹中创建一个名为“helper.php”的文件。 此文件包含类名作为帮助程序,它有助于在模块输出中显示检索到的数据。

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>IOWIKI</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 IOWIKI!!!!! </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创建模块

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

Joomla创建模块

Step (9) - 您可以将此模块分配为与其他模块类似,然后将其发布。

↑回到顶部↑
WIKI教程 @2018