Joomla! Hello

Free Joomla Tutorials and Resources


The one thing about a Web site, it always changes! Joomla! makes it easy to add Articles, content, images, videos, and more. Site administrators can edit and manage content 'in-context' by clicking the 'Edit' link. Webmasters can also edit content through a graphical Control Panel that gives you complete control over your site.

Home Tutorials Tiếng Việt - Vietnamese contents Tạo Module Hello World cho Joomla! 1.5
Tạo Module Hello World cho Joomla! 1.5 PDF Print E-mail
(8 Votes)
Sunday, 05 October 2008 15:31
Tạo Module Hello World cho Joomla! 1.5
Module là thành phần mở rộng gọn nhẹ và uyển chuyển được sử dụng để biểu diễn nội dung của trang thông tin. Chúng được sử dụng làm 1 thành phần nhỏ đơn giản trong các trang nội dung và quan trọng, nó có thể được xuất hiện cùng các component khác nhau.

Bạn có thể dễ dàng thấy nhiều ví dụ của menu trong thành phần cài đặt chuẩn của Joomla!: - menus - Tin mới nhất - form đăng nhập - và nhiều module khác nữa.
Bài viết này sẽ giải thích cho bạn cách tạo 1 module lời chào "Hello World". Qua bài hướng dẫn này bạn sẽ học cấu trúc cơ bản của một module. Cấu trúc đơn giản này sẽ đựoc mở rộng để tạo các module phức tạp hơn.

Cấu trúc file của 1 module

Có 4 file cơ bản được sử dụng trong mẫu phát triển module chuẩn:
  • mod_helloworld.php - Đây là file điểm nhập chính của module. Nó sẽ thực thi các hướng xác lập khởi đầu cần thiết cho nội dung module. Gọi các thủ tụ trợ giúp (helper) để thu thập các dữ liệu cần thiết, và bao gồm cả giao diện (template) sẽ được sử dụng để hiển thị nội dung xuất ra của module.
  • mod_helloworld.xml - File này có chứa thông tin của module. Nó định nghĩa các file cần thiết sẽ được cài đặt trong Joomla và chỉ định các tham số cấu hình (configuration parameters) đối với module.
  • helper.php - File này có chứa lớp trợ giúp (helper class) sẽ được sử dụng để thực hiện 1 tác vụ xác định nào đó như khôi phục/nhận dữ liệu mà sẽ được được hiển thị trong module (thường thường dữ liệu nhận về từ cơ sở dữ liệu (database) hoăch các nguồn khác).
  • tmpl/default.php - Đây là giao diện của module. File này sẽ nhận các dữ liệu đã thu thập bởi mod_helloworld.php và sinh mã HTML để hiển thị trên trang thông tin tại vị trí module.
Tạo mod_helloworld.php

 

File mod_helloworld.php sẽ thực hiện 3 nhiệm vụ sau:

  • Gọi file helper.php chứa lớp được dùng để thu thập dữ liệu cần thiết
  • Triệu gọi phương thức thích hợp trong lớp helper để lấy về dữ liệu
  • Gọi template để hiển thị đầu ra.

Lớp helper được định nghĩa trong file helper.php. File này được gọi bằng lệnh require_once:

require_once( dirname(__FILE__).DS.'helper.php' );

require_once được dùng bởi hàm helper được định nghĩa bên trong một lớp, và chúng ta muốn lớp này được định nghĩa chỉ 1 lần mà thôi.
Lớp helper hiện vẫn chưa được định nghĩa, nhưng khi được định nghĩa nó sẽ chứa một phương thức: getHello(). Trong ví dụ đơn giản này, không thực sự cần thiết phải làm như vậy – thông điệp “Hello, World” mà phương thức này trả về đơn giản là sẽ được gọi bên trong template. Chúng ta dùng lớp helper chỉ để minh họa kỹ thuật cơ bản này mà thôi.
Hiện tại thì module chưa sử dụng bất kỳ tham số nào, nhưng dù sao thì chúng ta cũng sẽ truyền cho phương thức helper để nó có thể sử dụng về sau, nếu chúng ta quyết định mở rộng chức năng cho module này.
Phương thức lớp helper được triệu gọi theo cách sau đây:
$hello = modHelloWorldHelper::getHello( $params );

File mod_helloworld.php hoàn chỉnh như sau:
/**
* Hello World! Module Entry Point
*
* @package Joomla.Tutorials
* @subpackage Modules
* @link http://dev.joomla.org/component/option,com_jd-wiki/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' ) );

Tạo helper.php

Có một dòng mà chúng ra vẫn chưa hề được nhắc đến chính là dòng đầu tiên. Dòng này kiểm tra để chắc chắn rằng file này thực sự được include từ một ứng dụng Joomla!. Nó thực sự cần thiết để tránh bị khai thác lỗi variable injection và các nguy cơ tiềm ẩn khác.

File helper.php chứa lớp helper dùng để lấy về dữ liệu cho đầu ra của module. Như sẽ đề cập, lớp helper của chúng ta sẽ có 1 method: getHello() trả về thông điệp ‘Hello, World’.

Và đây là code cho helper.php:

/**
* Helper class for Hello World! module
*
* @package Joomla.Tutorials
* @subpackage Modules
* @link http://dev.joomla.org/component/option,com_jd-wiki/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!';
}
}


Không có quy tắc nào bắt chúng ta phải đặt tên cho lớp helper như trên, nhưng đặt tên như vậy sẽ rất có ích cho việc nhận dạng lớp và tìm kiếm.
Các module cao cấp hơn có thể sẽ chứa các yêu cầu truy vấn database hoặc các chức năng khác.

Tạo tmpl/default.php

File default.php là một template nó sẽ hiển thị đầu ra của module.
Code cho default.php như sau:

defined( '_JEXEC' ) or die( 'Restricted access' ); ?>


Một điểm quan trọng cần chú ý là file template có cùng phạm vi với file mod_helloworld.php. Nghĩa là biến $hello được định nghĩa trong file mod_helloworld.php có thể dùng được trong file template default.php mà không cần bất kỳ khai báo hay lời gọi hàm nào.

Tạo mod_helloworld.xml

File mod_helloworld.xml được dùng để chỉ ra những file nào mà trình cài đặt cần phải copy và sẽ được sử dụng bởi Module Manager để xác định tham số nào được dùng để cấu hình cho module. Những thông tin khác về module cũng được định nghĩa trong file này.

Code cho mod_helloworld.xml như sau:



Hello, World!
John Doe
1.5.0
A simple Hello, World! module.

mod_helloworld.php
index.html
helper.php
tmpl/default.php
tmpl/index.html





Chú ý là có 2 file thêm vào mà chúng ra chưa nhắc đến đó là: index.html and tmpl/index.html. Các file này cũng được include để tránh cho các thư mục không bị truy cập và liệt kê nội dung bởi trình duyệt. Nếu người dùng cố gắng trỏ trình duyệt của họ tới thẳng các thư mục thì file index.html sẽ được hiển thị và họ sẽ chẳng thấy gì hết. Các file này có thể để trống hoặc chứa một dòng đơn giản sau:


Nó sẽ hiển thị một trang trắng tinh.
Vì module của chúng ta ko có parameters nào, nên chương này sẽ bỏ trống.

Kết luận

Phát triển module cho Joomla! là một việc khá đơn giản, trình tự rõ ràng. Bằng cách sử dụng kỹ thuật đã mô tả trong bài viết này, chúng ta có thể dễ dàng phát triển rất, rất nhiều các modules khác nữa.

Joomla! Hello biên dịch và tham khảo từ http://my.opera.com/trantrung22/

Last Updated on Tuesday, 14 October 2008 03:30
 

Comments

Great post.
B
i
u
Quote
Code
List
List=
[*]
URL
Name *
Email (For verification & Replies)
URL
Code   
ChronoComments by Joomla Professional Solutions
Submit Comment
Cancel
avatar Candace
0
 
 
Hi guys. Man is the only animal that can remain on friendly terms with the victims he intends to eat until he eats them. Help me! It has to find sites on the: drug addiction treatment clinic. I found only this - drug addiction treatment program. A proven new hampshire alcohol treatment plan that is holistic in. An alcoholic treatment center has professional staff that treats clients via medications and counseling so that alcohol addiction treatment approaches. Thanks :-(. Candace from Islands.
B
i
u
Quote
Code
List
List=
[*]
URL
Name *
Email (For verification & Replies)
URL
Code   
ChronoComments by Joomla Professional Solutions
Submit Comment
Cancel
avatar Pokupka Ssylok
0
 
 
Hi guys. Whenever you have an efficient government you have a dictatorship.
I am from Kiribati and know bad English, give please true I wrote the following sentence: "Specializes in search engine optimization and marketing.At seo drop we provide the tools to help identify areas of improvement for your website ranking and keywords on search engines."

With best wishes :-D, Venus.
B
i
u
Quote
Code
List
List=
[*]
URL
Name *
Email (For verification & Replies)
URL
Code   
ChronoComments by Joomla Professional Solutions
Submit Comment
Cancel
avatar Shea
0
 
 
Good afternoon. There are a terrible lot of lies going around the world, and the worst of it is half of them are true. Help me! Need information about: Buy fast delivery cialis. I found only this - buy generic Cialis. Here at buy propecia online viagra cialis and more. The buy legal fda approved viagra cast uniquely technology quit published and imposed with saliva conquer administered release of buy cialis free shipping. Thank you very much :eek:. Shea from Arab.
B
i
u
Quote
Code
List
List=
[*]
URL
Name *
Email (For verification & Replies)
URL
Code   
ChronoComments by Joomla Professional Solutions
Submit Comment
Cancel
Sorry. Man is distinguished from all other creatures by the faculty of laughter.
I am from East and also now am reading in English, give true I wrote the following sentence: "investments are not based often through people, social as methods, closed-end institutions, support quintiles, amount partnerships, infinitely-lived-representative-agent economy staffs, and prllp investors."

Thank :-( Financial and investment advice.
B
i
u
Quote
Code
List
List=
[*]
URL
Name *
Email (For verification & Replies)
URL
Code   
ChronoComments by Joomla Professional Solutions
Submit Comment
Cancel
Sorry. Students achieving Oneness will move on to Twoness.
I am from Papua and now study English, give true I wrote the following sentence: "Gus levy developed the event in the distributions as a items timing, which disputed a photo at goldman where there would be two savages collectively bringing for stock, one from language amount and one from launches style."

Thanks for the help :), Best online share trading.
B
i
u
Quote
Code
List
List=
[*]
URL
Name *
Email (For verification & Replies)
URL
Code   
ChronoComments by Joomla Professional Solutions
Submit Comment
Cancel
I want to take this opportunity to say that I really love this blog. It has been a good resource of information for me in my research. Thank you so much admin.
B
i
u
Quote
Code
List
List=
[*]
URL
Name *
Email (For verification & Replies)
URL
Code   
ChronoComments by Joomla Professional Solutions
Submit Comment
Cancel
avatar Jennifer
0
 
 
Buy Cheap Lipitor Euro, Discount Lipitor Internet Pharmacy UK Super ED Trial Pack Europe, Sale Super ED Trial Pack Internet Pharmacy Purchase Island Viagra Professional in Europe, Cheap price Viagra Professional Internet Pharmacy
B
i
u
Quote
Code
List
List=
[*]
URL
Name *
Email (For verification & Replies)
URL
Code   
ChronoComments by Joomla Professional Solutions
Submit Comment
Cancel
avatar tad
0
 
 
Well written! I searched for some tutorials at http://www.picktorrent.com , but found nothing informative, your article helped me much!
B
i
u
Quote
Code
List
List=
[*]
URL
Name *
Email (For verification & Replies)
URL
Code   
ChronoComments by Joomla Professional Solutions
Submit Comment
Cancel
Phát triển module cho Joomla! là một việc khá đơn giản, trình tự rõ ràng. Bằng cách sử dụng kỹ thuật đã mô tả trong bài viết này, chúng ta có thể dễ dàng phát triển rất, rất nhiều các modules khác nữa.
B
i
u
Quote
Code
List
List=
[*]
URL
Name *
Email (For verification & Replies)
URL
Code   
ChronoComments by Joomla Professional Solutions
Submit Comment
Cancel
avatar chat software
0
 
 
So far, I managed to go though only some of posts you discuss here, but I find them very interesting and informative. Just want say thank you for the information you have shared. Regards
B
i
u
Quote
Code
List
List=
[*]
URL
Name *
Email (For verification & Replies)
URL
Code   
ChronoComments by Joomla Professional Solutions
Submit Comment
Cancel
avatar Oster
0
 
 
Chú ý là có 2 file thêm vào mà chúng ra chưa nhắc đến đó là: index.html and tmpl/index.html. Các file này cũng được include để tránh cho các thư mục không bị truy cập và liệt kê nội dung bởi trình duyệt. Nếu người dùng cố gắng trỏ trình duyệt của họ tới thẳng các thư mục thì file index.html sẽ được hiển thị và họ sẽ chẳng thấy gì hết. Các file này có thể để trống hoặc chứa một dòng đơn giản sau:
B
i
u
Quote
Code
List
List=
[*]
URL
Name *
Email (For verification & Replies)
URL
Code   
ChronoComments by Joomla Professional Solutions
Submit Comment
Cancel
This is good site to spent time on .I just stumbled upon your informative blog and wanted to say that I have really enjoyed reading your very well written blog posts. I will be your frequent visitor, that’s for sure.
B
i
u
Quote
Code
List
List=
[*]
URL
Name *
Email (For verification & Replies)
URL
Code   
ChronoComments by Joomla Professional Solutions
Submit Comment
Cancel
Great blog, just looking around some blogs, seems a pretty nice platform you are using. I'm currently using Wordpress for a few of my sites but looking to change one of them over to a platform similar to yours as a trial run. Anything in particular you would recommend about it?

B
i
u
Quote
Code
List
List=
[*]
URL
Name *
Email (For verification & Replies)
URL
Code   
ChronoComments by Joomla Professional Solutions
Submit Comment
Cancel
Great read. I enjoyed reading your post and I like your take on the issue. Thanks.
B
i
u
Quote
Code
List
List=
[*]
URL
Name *
Email (For verification & Replies)
URL
Code   
ChronoComments by Joomla Professional Solutions
Submit Comment
Cancel
Thanks for this very useful info you have provided us. I will bookmark this for future reference and refer it to my friends. More power to your blog.

B
i
u
Quote
Code
List
List=
[*]
URL
Name *
Email (For verification & Replies)
URL
Code   
ChronoComments by Joomla Professional Solutions
Submit Comment
Cancel
I am just new to your blog and just spent about 1 hour and 30 minutes lurking and reading. I think I will frequent your blog from now on after going through some of your posts. I will definitely learn a lot from them. Regards - Gerry
B
i
u
Quote
Code
List
List=
[*]
URL
Name *
Email (For verification & Replies)
URL
Code   
ChronoComments by Joomla Professional Solutions
Submit Comment
Cancel
avatar resume
0
 
 
Thanks for sharing this information. I'm not adept in this kind of programming language and I don't have time to study in a formal school. I've just resorted to browsing the web for helpful tips such as this one.
B
i
u
Quote
Code
List
List=
[*]
URL
Name *
Email (For verification & Replies)
URL
Code   
ChronoComments by Joomla Professional Solutions
Submit Comment
Cancel
B
i
u
Quote
Code
List
List=
[*]
URL
Name *
Email (For verification & Replies)
URL
Code   
ChronoComments by Joomla Professional Solutions
Submit Comment
Working....
Finished
Failed