สิ่งที่ต้องรู้ก่อน
- MVC ใน Joomla
- การสร้าง Component Joomla 2.5 พัฒนา Component บน Joomla Framework Extention
ปกติการแสดงผลของ Component จะใช้ function display() ใน Class ของ Controller
โค้ด: เลือกทั้งหมด
class TestControllerTest extends JControllerLegacy
{
function display($cachable = false, $urlparams = array())
{
JRequest::setVar('view', 'test'); // เลือก view ชื่อ test
parent::display($cachable, $urlparams);
}
}
- JRequest::setVar('view', 'test');
- $this->input->set('view', 'test');
หมายเหตุ เปลี่ยน test เป็นชื่อ View ที่ต้องการ
ในส่วน View จะประกอบด้วยไฟล์ Layout ค่าเริ่มต้นคือไฟล์ default ถ้าต้องการเลือก Layout อื่นๆ นอกจากตัว default สามารถเลือกโดยใช้ 2 คำสั่งด้านบน เปลี่ยน view เป็น layout เช่น ต้องการเลือก layout ชื่อ hello
โค้ด: เลือกทั้งหมด
class TestControllerTest extends JControllerLegacy
{
function display($cachable = false, $urlparams = array())
{
JRequest::setVar('view', 'test'); // เลือก view ชื่อ test
JRequest::setVar('layout', 'hello'); // เลือก layout ชื่อ hello
parent::display($cachable, $urlparams);
}
}
ใน Class ของส่วน View ก็จะใช้ function display() เช่นกัน
โค้ด: เลือกทั้งหมด
class TestViewTest extends JViewLegacy
{
function display($tpl = null)
{
parent::display($tpl);
}
}
โค้ด: เลือกทั้งหมด
class TestControllerTest extends JControllerLegacy
{
function display($cachable = false, $urlparams = array())
{
$view = $this->getView('test', 'html');
$view->hello(); // ชี้เลือก function ใน view
parent::display($cachable, $urlparams);
}
}
ถ้าต้องการเลือก Layout ใช้ $view->setLayout('***'); (*** = ชื่อ layout)
Class View
โค้ด: เลือกทั้งหมด
class TestViewTest extends JViewLegacy
{
function display($tpl = null)
{
parent::display($tpl);
}
function hello()
{
echo 'Hello World';
}
}
โค้ด: เลือกทั้งหมด
class TestControllerTest extends JControllerLegacy
{
function display($cachable = false, $urlparams = array())
{
$view = $this->getView( 'test', 'html' );
$view->setModel($this->getModel('test'), true); // เรียกใช้ Model ชื่อ test (ค่า true เป็นการ set default model)
$view->setModel($this->getModel('testa')); // เรียกใช้ Model ชื่อ testa
parent::display($cachable, $urlparams);
}
}
โค้ด: เลือกทั้งหมด
class TestViewTest extends JViewLegacy
{
function display($tpl = null)
{
$item1 = $this->get('data' ); // เรียกใช้ function getData() ใน Model ชื่อ test
$item2 = $this->get('data', 'testa'); // เรียกใช้ function getData() ใน Model ชื่อ testa
parent::display($tpl);
}
}