2. Nhắc lại bài cũ
• Chỉnh sửa template thông qua chỉnh sửa hình ảnh, chỉnh
sửa CSS
• Cấu trúc file và thư mục của một Template
Bài 5 - Hướng dẫn xây dựng Extension
3. Mục tiêu bài học
• Hiểu rõ cấu trúc của component; module
• Hiểu rõ về quy trình, cách thức, giải pháp xây dựng
component, module
Bài 5 - Hướng dẫn xây dựng Extension
4. Xây dựng Component
Xây dựng component theo mô hình MVC
- Các component Joomla được xây dựng theo mô hình MVC
(Model-View-Controler);
User
(Khách truy
cập web)
Bài 5 - Hướng dẫn xây dựng Extension
View
(tạo giao diện hiển
thị)
Model
(thiết lập các
chức năng web)
Controler
(điều khiển, xử lý
tương tác)
6. Xây dựng Component
Component cơ bản có 5 file:
• site/hello.php - file tạo entry point
• site/controller.php - Thiết lập điều khiển
• site/views/hello/view.html.php - Thiết lập
hiển thị
• site/views/hello/tmpl/default.php - Tạo giao
diện hiển thị
• hello.xml - Đóng gói thành bộ cài
Bài 5 - Hướng dẫn xây dựng Extension
• site/hello.php - file tạo entry point
• site/controller.php - Thiết lập điều khiển
• site/views/hello/view.html.php - Thiết lập
hiển thị
• site/views/hello/tmpl/default.php - Tạo giao
diện hiển thị
• hello.xml - Đóng gói thành bộ cài
7. Xây dựng Component
Lập trình file Hello.php - Tạo entry point
<?php
defined( '_JEXEC' ) or die( 'Restricted access' );
require_once( JPATH_COMPONENT.DS.'controller.php' );
if ($controller = JRequest::getWord('controller')) {
$path = JPATH_COMPONENT.DS.'controllers'.DS.$controller.'.php';
if (file_exists($path)) {
require_once $path;
} else {
$controller = '';
}
}
$classname = 'HelloController'.$controller;
$controller = new $classname();
$controller->execute( JRequest::getVar( 'task' ) );
$controller->redirect();
Bài 5 - Hướng dẫn xây dựng Extension
<?php
defined( '_JEXEC' ) or die( 'Restricted access' );
require_once( JPATH_COMPONENT.DS.'controller.php' );
if ($controller = JRequest::getWord('controller')) {
$path = JPATH_COMPONENT.DS.'controllers'.DS.$controller.'.php';
if (file_exists($path)) {
require_once $path;
} else {
$controller = '';
}
}
$classname = 'HelloController'.$controller;
$controller = new $classname();
$controller->execute( JRequest::getVar( 'task' ) );
$controller->redirect();
8. Xây dựng Component
Tạo controller với file controller.php
<?php
defined( '_JEXEC' ) or die( 'Restricted access' );
jimport('joomla.application.component.controller');
class HelloController extends JController
{
function display()
{
parent::display();
}
}
Bài 5 - Hướng dẫn xây dựng Extension
<?php
defined( '_JEXEC' ) or die( 'Restricted access' );
jimport('joomla.application.component.controller');
class HelloController extends JController
{
function display()
{
parent::display();
}
}
9. Xây dựng Component
Tạo view - lập trình file site/views/hello/view.html.php
<?php
defined( '_JEXEC' ) or die( 'Restricted access' );
jimport( 'joomla.application.component.view');
class HelloViewHello extends JView
{
function display($tpl = null)
{
$greeting = "Hello, World!";
$this->assignRef( 'greeting', $greeting );
parent::display($tpl);
}
}
Bài 5 - Hướng dẫn xây dựng Extension
<?php
defined( '_JEXEC' ) or die( 'Restricted access' );
jimport( 'joomla.application.component.view');
class HelloViewHello extends JView
{
function display($tpl = null)
{
$greeting = "Hello, World!";
$this->assignRef( 'greeting', $greeting );
parent::display($tpl);
}
}
10. Xây dựng Component
Tạo Template tại file site/views/hello/tmpl/default.php
<?php defined('_JEXEC') or die('Restricted access'); ?>
<h1><?php echo $this->greeting; ?></h1>
Bài 5 - Hướng dẫn xây dựng Extension
22. Xây dựng Module
Tạo file index.html trong các thư mục của module để
bảo mật với nội dung:
<html><body bgcolor="#FFFFFF"></body></html>
Bài 5 - Hướng dẫn xây dựng Extension
23. Tổng kết bài học
• Các component trong Joomla được xây dựng theo mô
hình MVC và dựa vào Joomla Framework - thư viện mã
nguồn sẵn có trong Joomla CMS.
• Quy trình xây dựng giống nhau đối với tất cả các
component hay module.
• Sau khi hoàn thiện lập trình một component hay module,
cần đóng gói thành file .zip để có thể cài đặt vào Joomla
từ trình cài đặt tháo gỡ tự động của Joomla
• Các component trong Joomla được xây dựng theo mô
hình MVC và dựa vào Joomla Framework - thư viện mã
nguồn sẵn có trong Joomla CMS.
• Quy trình xây dựng giống nhau đối với tất cả các
component hay module.
• Sau khi hoàn thiện lập trình một component hay module,
cần đóng gói thành file .zip để có thể cài đặt vào Joomla
từ trình cài đặt tháo gỡ tự động của Joomla
Bài 5 - Hướng dẫn xây dựng Extension