You are on page 1of 5

To Module cho Joomla! 1.

5
Module l mt extension rt nh v mm do c s dng render nn trang web. Chng c s dng cho trang c kch thc nh, t phc tp v c th c xem t nhiu component khc nhau. Bn c th thy nhiu v d v module trong bn ci t Joomla! chun nh: - Menus - Latest News - Login form - v nhiu ci khc na. Bi vit ny s hng dn bn cch to ra mt module Hello World n gin. Xuyn sut bi vit ny bn s hc c cu trc c bn ca mt module. Tt nhin, bn hon ton c th m rng cu trc c bn ny to ra cc module phc tp hn theo mnh. Cu trc tp tin C 4 file c bn c dng trong mu tiu chun ca vic pht trin module: mod_helloworld.php File ny l im vo chnh cho module. N s thc hin nhng khi to cn thit, gi cc helper thu thp mi d liu cn thit, v include template hin th u ra ca module. mod_helloworld.xml File ny cha thng tin v module. N nh ngha ra cc files cn thit ci t t trnh ci t ca Joomla! v ch ra cc tham s cu hnh cho module. helper.php File ny cha lp helper, c dng thc hin cc hnh ng thc s ly v cc thng tin m s c hin th trn module (thng l t database hoc nhng ngun d liu
www.dvms.vn Page 1

khc). tmpl/default.php File ny l template ca module. N s ly v d liu c thu thp bi mod_helloworld.php v sinh ra m HTML hin th trn trang web.

To mod_helloworld.php
File mod_helloworld.php s thc hin 3 nhim v sau: include file helper.php cha lp c dng thu thp d liu cn thit Triu gi phng thc thch hp trong lp helper ly v d liu include template hin th u ra. Lp helper c nh ngha trong file helper.php ca chng ta. File ny c include bng lnh require_once:
require_once( dirname(__FILE__).DS.'helper.php' );

require_once c dng bi hm helper ca chng ta c nh ngha bn trong mt lp, v chng ta mun lp ny c nh ngha ch 1 ln m thi. Lp helper ca chng ta hin vn cha c nh ngha, nhng khi c nh ngha n s cha mt phng thc: getHello(). Vi v d n gin ca chng ta, khng thc s cn thit phi lm nh vy thng ip Hello, World m phng thc ny tr v n gin l s c include vo trong template. Chng ta dng lp helper ch chng minh k thut c bn ny m thi. Hin ti th module ca chng ta cha s dng bt k tham s no, nhng d sao th chng ta cng s truyn cho phng thc helper n c th s dng v sau, nu chng ta quyt nh m rng chc nng cho module ny. Phng thc lp helper c triu gi theo cch sau y:
$hello = modHelloWorldHelper::getHello( $params );

File mod_helloworld.php hon chnh nh sau:


<?php /** * Hello World! Module Entry Point * * @package Joomla.Tutorials * @subpackage Modules * @link http://dev.joomla.org/component/option,com_jdwiki/Itemid,31/id,tutorials:modules/ * @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. */ // no direct access defined( '_JEXEC' ) or die( 'Restricted access' ); // Include the syndicate functions only once require_once( dirname(__FILE__).DS.'helper.php' ); $hello = modHelloWorldHelper::getHello( $params ); require( JModuleHelper::getLayoutPath( 'mod_helloworld' ) );

C mt dng m chng ra vn cha h ng n chnh l dng u tin. Dng ny kim tra chc chn rng file ny thc s c include t mt ng dng Joomla!. N thc s cn thit
www.dvms.vn Page 2

trnh b khai thc li variable injection v cc nguy c tim n khc.

To helper.php
File helper.php cha lp helper dng ly v d liu cho u ra ca module. Nh s cp, lp helper ca chng ta s c 1 method: getHello() tr v thng ip Hello, World. V y l code cho helper.php:
<?php /** * Helper class for Hello World! module * * @package Joomla.Tutorials * @subpackage Modules * @link http://dev.joomla.org/component/option,com_jdwiki/Itemid,31/id,tutorials:modules/ * @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 */ function getHello( $params ) { return 'Hello, World!'; } }

Khng c quy tc no bt chng ta phi t tn cho lp helper nh trn, nhng t tn nh vy s rt c ch cho vic nhn dng lp v tm kim. Cc module cao cp hn c th s cha cc yu cu truy vn database hoc cc chc nng khc.

To tmpl/default.php
File default.php l mt template n s hin th u ra ca module. Code cho default.php nh sau:
<?php // no direct access defined( '_JEXEC' ) or die( 'Restricted access' ); ?> <?php echo $hello; ?>

Mt im quan trng cn ch l file template c cng phm vi vi file mod_helloworld.php. Ngha l bin $hello c nh ngha trong file mod_helloworld.php c th dng c trong file template default.php m khng cn bt k khai bo hay li gi hm no.

To mod_helloworld.xml
File mod_helloworld.xml c dng ch ra nhng file no m trnh ci t cn phi copy v s c s dng bi Module Manager xc nh tham s no c dng cu hnh cho

www.dvms.vn

Page 3

module. Nhng thng tin khc v module cng c nh ngha trong file ny. Code cho mod_helloworld.xml nh sau:
<?xml version="1.0" encoding="utf-8"?> <install type="module" version="1.5.0"> <name>Hello, World!</name> <author>John Doe</author> <version>1.5.0</version> <description>A simple Hello, World! module.</description> <files> <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> <params> </params> </install>

Ch l c 2 file thm vo m chng ra cha nhc n l: index.html and tmpl/index.html. Cc file ny cng c include trnh cho cc th mc khng b truy cp v lit k ni dung bi trnh duyt. Nu ngi dng c gng tr trnh duyt ca h ti thng cc th mc th file index.html s c hin th v h s chng thy g ht. Cc file ny c th trng hoc cha mt dng n gin sau:
<html><body bgcolor="#FFFFFF"></body></html>

N s hin th mt trang trng tinh. V module ca chng ta ko c parameters no, nn chng ny s b trng.

Kt lun
Pht trin module cho Joomla! l mt vic kh n gin, trnh t r rng. Bng cch s dng k thut m t trong bi vit ny, chng ta c th d dng pht trin rt, rt nhiu cc modules khc na. Dch t Joomla Developer Site

CHC THNH CNG!

THNG TIN LIN H H TR


DVMS (08) 360 289 37 info@dvms.vn www.DVMS.vn
www.dvms.vn Page 4

www.dvms.vn

Page 5