Hướng dẫn viết module cho phiên bản NukeViet 3.0

1. Cấu trúc cơ bản khi bắt đầu viết module
Khác với những phiên bản trước đây của hệ thống nukeviet, trong phiên bản mới này việc đóng gói module được tối ưu, các file và folder của module đều được đưa vào trong 1 thư mục.
Một module example có cơ sở dữ liệu và có phần quản trị cho người dùng có cấu trúc căn bản như sau:

admin
—-.htaccess
—-main.php
modules
funcs
—-index.html
—-.htaccess
—-main.php
language
—-index.html
—-.htaccess
—-vi.php
—-vi_admin.php
js
—-.htaccess
—-javascript.js
action.php
admin.functions.php
functions.php
version.php
index.html
.htaccess

2. Cách viết một module đơn giản
Chú ý: Tên module mới chỉ gồm các chữ cái, số và dấu gạch ngang, bắt buộc phải bắt đầu bằng một chữ cái.
- Trong bài viết này chúng ta xây dựng một module đơn giản để xuất ra nội dung,:
Hello world!!!
- Các bước chuẩn bị:
Tạo các file và folder như sau:

$ modules/example/index.html
$ modules/example/funcs/main.php
$ modules/example/funcs/index.html
$ modules/example/admin.functions.php
$ modules/example/functions.php
$ modules/example/theme.php
$ modules/example/version.php

modules/example/functions.php
modules/example/version.php
Các chú ý khi khai báo trong file version.php
- “modfuncs” => “main”, là danh sách các các function tương ứng với tên các file trong folder funcs của module.
- “virtual” => 0: Module không được ảo hóa
- “virtual”=>1: Module được ảo hóa. modules/example/funcs/main.php

Sau khi thực hiện các bước như trên ta có được 1 module đơn giản, Để xem kết quả bạn đăng nhập vào khu vực quản trị với quyền admin, cài đặt và click hoạt module để có thể xem module qua link:
http://mydomain.com/index.php?lang=vi&nv=example
Nếu bạn muốn thêm 1 func mới cho module bạn tạo mới 1 file (new.php) trong thư mục:
modules/example/funcs/

Sau đó bạn cần đăng nhập và khu vực quản trị kích hoạt các function này.
3. Thêm link quản lý module vào menu quản trị
modules/example/admin/functions.php

modules/example/admin/main.php
Để có thể quản trị module thì trong thư mục admin bắt buộc phải chứa file: main.php và trong thư mục
tương ứng với các file functions.php, version.php bắt buộc phải chứa file: admin.functions.php

Chú ý: Để các function có thể hoạt động được thì function phải được gán vào mảng $allow_func
Khi đó danh mục các file của module gồm:
$ modules/example/admin/index.html
$ modules/example/admin/functions.php
$ modules/example/admin/main.php
$ modules/example/index.html
$ modules/example/funcs/main.php
$ modules/example/funcs/index.html
$ modules/example/functions.php
$ modules/example/version.php

4. Thêm 1 submenu quản lý module trong admin
Thay đổi lại file modules/example/admin.functions.php

5. Kết nối với file ngôn ngữ
Sửa lại nội dung file modules/example/admin.functions.php

Quy tắc đặt tên file ngôn ngữ: Các file ngôn ngữ được đặt trong thư mục language của module, Nếu ngôn ngữ cho module sẽ đặt tên: vi.php, en.php, ngôn ngữ cho admin admin_vi.php, admin_en.php
Ta tạo file ngôn ngữ tiếng Việt cho module example:
modules/example/language/ admin_vi.php

Khi đó danh mục các file của module gồm:
$ modules/example/admin/index.html
$ modules/example/admin/main.php
$ modules/example/index.html
$ modules/example/funcs/main.php
$ modules/example/funcs/index.html
$ modules/example/admin.functions.php
$ modules/example/functions.php
$ modules/example/theme.php
$ modules/example/version.php
$ modules/example/language/vi_admin.php
$ modules/example/language/en_admin.php

6. Kết nối với cơ sở dữ liệu
modules/example/action.php
Sau khi tạo xong file action.php, bạn vào phần quản lý module để cài lại module example. Khi đó cơ
sở dữ liệu sẽ được tạo tự động.

Sửa lại file modules/example/admin/add.php
04 {
06 }
12 {
15 {
17 }
19 {
23 {
28 }
30 {
32 }
33 }
34 }
39 {
44 }
70 ?>
Sửa lại file modules/example/language/vi_admin.php
Sửa lại file modules/example/admin/main.php
05 }
20 {
33 }
38 ?>
Sửa lại file modules/example/funcs/main.php
Danh mục các file:
$ modules/example/admin/index.html
$ modules/example/admin/main.php
$ modules/example/index.html
$ modules/example/funcs/main.php
$ modules/example/funcs/index.html
$ modules/example/admin.functions.php
$ modules/example/functions.php
$ modules/example/action.php
$ modules/example/language/vi_admin.php
$ modules/example/language/en_admin.php

7. Kết nối với template engine (Xtemplate)
Bắt đầu từ phiên bản nukeviet 3.0, hệ thống template được tách biệt hẳn ra với mã php. Hiện tại, hệ thống template của nukeviet đang sử dụng Xtemplate để xử lý cho việc tách biệt mã php và html. Để tìm hiểu thêm về cách viết Xtemplate, các bạn có thể đọc thêm tài liệu về tại địa chỉ: http://sourceforge.net/projects/xtpl/files/XTemplate%20PHP5/
Sơ đồ template admin control panel sử dụng Xtemplate

themes
—-admin_default
css
—-example.css
images
js
modules
—-example.php
tpl
—-example.tpl

Để sử dụng Xtemplate cho admin control panel của module Example ta tiến hành các bước như sau:
Sửa lại file modules/example/admin/main.php

Sửa lại file modules/example/admin/add.php
Sửa lại file modules/example/admin/edit.php
Thêm file themes/admin_default/modules/example.php. Lưu ý: Tên file trong thư mục module của
template admin phải trùng tên với tên module.

03 {
14 {
17 }
21 }
23 {
36 {
39 }
43 }
45 {
52 {
54 {
64 }
65 }
68 }
69 ?>
Thêm file themes/admin_default/modules/example.tpl
Danh sách các file:
$ modules/example/admin/index.html
$ modules/example/admin/main.php
$ modules/example/index.html
$ modules/example/funcs/main.php
$ modules/example/funcs/index.html
$ modules/example/admin.functions.php
$ modules/example/functions.php
$ modules/example/action.php
$ modules/example/language/vi_admin.php
$ modules/example/language/en_admin.php
$ themes/admin_default/modules/example.php
$ themes/admin_default/modules/example.tpl

Sơ đồ cấu trúc template sử dụng Xtemplate
themes
—-default
css
—-example.css
images
js
modules
—-example.php
tpl
—-example.tpl

Sửa lại file modules/example/funcs/main.php
Thêm mới file modules/example/theme.php với nội dung như sau:
Chú ý:Tên của file trong thư mục modules của template phải trùng với tên của module.

Thêm mới file themes/default/modules/example/example.xtpl
Nếu bạn cần tạo ngôn ngữ cho ngoài site bạn có thể tạo thêm các file: vi.php, en.php trong thư
mục language của module

Khi đó danh mục các file của module gồm:
$ modules/example/admin/index.html
$ modules/example/admin/main.php
$ modules/example/index.html
$ modules/example/funcs/main.php
$ modules/example/funcs/index.html
$ modules/example/admin.functions.php
$ modules/example/functions.php
$ modules/example/theme.php
$ modules/example/action.php
$ modules/example/version.php
$ modules/example/language/vi.php
$ modules/example/language/vi_admin.php
$ modules/example/language/en.php
$ modules/example/language/en_admin.php
$ themes/default/modules/example.php
$ themes/default/modules/example/example.xtpl
$ themes/admin_default/modules/example.php
$ themes/admin_default/modules/example.tpl

Các chú ý khi lập trình
1) Encoding
Encoding mặc định tất cả các file là: ansi, các file lang như tiếng việt, pháp, nga .. Encoding cần chuyển sang utf-8
2) Trigger_error
thay vì dùng die(“…”) báo lỗi và dừng chương trình; cần dùng dùng trigger_error(“…”, 256);
để ghi lại lỗi đó và thông báo lỗi.

3) Chế độ thông báo lỗi
Để thay đổi chế độ thông báo lỗi của php cần sửa file includes /constants.php
VD: để tắt thông báo lỗi ta cần sửa đoạn trên thành
Nhung cai trang nao ko cho phep truy cap truc tiep neu khong co referer can phai co dong nay
4) Lấy giá trị của biến khi submit form
class request còn có thể lấy theo các phương thức: get_bool, get_string, get_int, get_float, get_array, Trong phương thức get_string nếu lấy qua phương thức get, giá trị của biến đã được lọc mã html
Lấy biến dụng số nguyên

Lấy biến dụng số thực
Sử dụng hàm: filter_text_input
23 ?>
Sử dụng hàm: filter_text_textarea
khi 1 form có textarea, chỉ dùng phương thức post, chứ không dùng phương thức get, nếu không sẽ bị mất dữ liệu
sử dụng filter_text_textarea cho các editor

sử dụng filter_text_textarea cho các textarea không sử dụng editor
14 ?>

0 nhận xét:

Đăng nhận xét