ส่วน view คืออะไร ถ้าไม่เข้าใจศึกษาแนวคิดนะครับ
แนวคิด MVC Joomla
ก่อนอื่นต้องมีไฟล์ตามขั้นตอนนี้มาก่อน
สร้าง Component สำหรับ Joomla 2.5 แบบพื้นฐาน
1. ไปที่โฟลเดอร์ helloworld ที่เราสร้างไว้ในหัวข้อที่แ้ล้ว เข้าไปที่ site -> helloworld.php เปิดไฟล์ขึ้นมา เขียนโค้ดลงไปครับ
โค้ด: เลือกทั้งหมด
<?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();
?>
2. ในโฟลเดอร์ site ให้สร้างไฟล์ controller.php แล้วใส่โค้ด
โค้ด: เลือกทั้งหมด
<?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
{
}
?>
3. สร้างโฟลเดอร์ views เปิดเข้าไป สร้างโฟลเดอร์ชื่อ helloworld เปิดเข้าไป สร้างไฟล์ view.html.php ใส่โค้ด
โค้ด: เลือกทั้งหมด
<?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);
}
}
?>
4. ให้สร้างโฟลเดอร์ tmpl เปิดเข้าไป สร้างไฟล์ default.php ใส่โค้ด
โค้ด: เลือกทั้งหมด
<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
?>
<h1><?php echo $this->msg; ?></h1>
?>
5. เปิดไฟล์ helloworld.xml ขึ้นมาแก้
เปลี่ยนเป็น
และ
โค้ด: เลือกทั้งหมด
<files folder="site">
<filename>index.html</filename>
<filename>helloworld.php</filename>
</files>
เปลี่ยนเป็น
โค้ด: เลือกทั้งหมด
<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