แนวคิด MVC Joomla
ก่อนอื่นต้องมีไฟล์ตามขั้นตอนนี้มาก่อน สร้าง Component สำหรับ Joomla 2.5 แบบพื้นฐาน
1. ไปที่โฟลเดอร์ helloworld ที่เราสร้างไว้ในหัวข้อที่แ้ล้ว เข้าไปที่ site -> helloworld.php เปิดไฟล์ขึ้นมา เขียนโค้ดลงไปครับ
Code: Select all
<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
// import joomla controller library
jimport('joomla.application.component.controller');
// Get an instance of the controller prefixed by HelloWorld
$controller = JController::getInstance('HelloWorld');
// Perform the Request task
$controller->execute(JRequest::getCmd('task'));
// Redirect if set by the controller
$controller->redirect();
?>
Code: Select all
<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
// import Joomla controller library
jimport('joomla.application.component.controller');
/**
* Hello World Component Controller
*/
class HelloWorldController extends JController
{
}
?>
Code: Select all
<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
// import Joomla view library
jimport('joomla.application.component.view');
/**
* HTML View class for the HelloWorld Component
*/
class HelloWorldViewHelloWorld extends JView
{
// Overwriting JView display method
function display($tpl = null)
{
// Assign data to the view
$this->msg = 'Hello World';
// Display the view
parent::display($tpl);
}
}
?>
Code: Select all
<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
?>
<h1><?php echo $this->msg; ?></h1>
?>
Code: Select all
<version>0.0.1</version>
Code: Select all
<version>0.0.2</version>
Code: Select all
<files folder="site">
<filename>index.html</filename>
<filename>helloworld.php</filename>
</files>
Code: Select all
<files folder="site">
<filename>index.html</filename>
<filename>helloworld.php</filename>
<filename>controller.php</filename>
<folder>views</folder>
</files>
6. ทำเป็น .zip แล้ว เข้าไปหน้าของ administrator ไปเพิ่ม extensions เ้ข้าไปใหม่
รันโค้ดโดยพิม URL = index.php?option=com_helloworld