ก่อนอื่นควรทำความเข้าใจกับ แนวคิด MVC กับการใช้งานใน joomla คลิกเข้าไปอ่านได้ครับ
- Joomla_mvc-diagram.png (37.45 KiB) Viewed 907 times
จากรูปจะขอสรุปส่วนการทำงานของ Components โดยแบ่งเป็น 3 ส่วน (ตามการเขียนโปรแกรมในรูปแบบ MVC) คือ
ส่วนของ Controller จะเรียกใช้งานตาม task ที่มีการส่งข้อมูลเข้ามา ซึ่ง Controller จะเป็นตัวกำหนด Model และ View ที่จะใช้งาน
ส่วนของ Model จะเป็นส่วนที่ติดต่อกับฐานข้อมูล การดึงข้อมูล เพิ่ม ลบ แก้ไข ทั้งหมดจะอยู่ภายในนี้ ในส่วน Controller และ View จะไม่มีการเรียกใช้ฐานข้อมูล
ส่วนของ View เป็นส่วนแสดงผลไปยังผู้ใช้งาน ซึ่งใน View อาจจะมีได้หลาย Layout Controller จะสามารถกำหนด Layout ของ View ได้
สำหรับตัวอย่างโครงสร้าง Component ต่อไปนี้จะเป็น Component ทางฝั่ง Admin ครับ ลุย !!!
เปิดโฟลเดอร์เข้าไปที่ administrator/components/ แล้วสร้างโฟลเดอร์ component ที่ต้องการ โดยการตั้งชื่อโฟลเดอร์ต้องขึ้นด้วย com_ เช่น ตั้งชื่อ hello ก็เป็น com_hello
ตัวอย่างไฟล์โครงสร้าง สร้างไฟล์ และ โฟลเดอร์ ตามรายการด้านล่าง
โค้ด: เลือกทั้งหมด
com_myjoomla
- controllers
- helpers
- language
- - en-GB
- - - en-GB.com_myjoomla.ini
- - - en-GB.com_myjoomla.sys.ini
- models
- tables
- views
- - myjoomla
- - - tmpl
- - - - default.php
- - - view.html.php
- controller.php
- myjoomla.php
- myjoomla.xml
ตัวอย่างโค้ด
- com_myjoomla/myjoomla.xml
โค้ด: เลือกทั้งหมด
<?xml version="1.0" encoding="utf-8"?>
<extension type="component" version="3.5" method="upgrade">
<name>com_myjoomla</name>
<author>Mindphp Developer Teams</author>
<creationDate>April 2016</creationDate>
<copyright>(C) 2005 - 2016 Open Source Matters. All rights reserved.</copyright>
<license>GNU General Public License version 2 or later; see LICENSE.txt</license>
<authorEmail>[email protected]</authorEmail>
<authorUrl>www.mindphp.com</authorUrl>
<version>1.0.0</version>
<administration>
<menu>com_myjoomla</menu>
<files folder="admin">
<filename>controller.php</filename>
<filename>myjoomla.php</filename>
<folder>controllers</folder>
<folder>helpers</folder>
<folder>language</folder>
<folder>models</folder>
<folder>tables</folder>
<folder>views</folder>
</files>
</administration>
</extension>
- com_myjoomla/myjoomla.php
โค้ด: เลือกทั้งหมด
defined('_JEXEC') or die;
JHtml::_('behavior.tabstate');
if (!JFactory::getUser()->authorise('core.manage', 'com_myjoomla'))
{
return JError::raiseWarning(404, JText::_('JERROR_ALERTNOAUTHOR'));
}
$controller = JControllerLegacy::getInstance('Myjoomla');
$controller->execute(JFactory::getApplication()->input->get('task'));
$controller->redirect();
- com_myjoomla/controller.php
โค้ด: เลือกทั้งหมด
defined('_JEXEC') or die;
class MyjoomlaController extends JControllerLegacy
{
}
- com_myjoomla/language/en-GB/en-GB.com_myjoomla.ini
- com_myjoomla/language/en-GB/en-GB.com_myjoomla.sys.ini
- com_myjoomla/views/myjoomla/view.html.php
โค้ด: เลือกทั้งหมด
defined('_JEXEC') or die;
class MyjoomlaViewMyjoomla extends JViewLegacy
{
public function display($tpl = null)
{
parent::display($tpl);
}
}
- com_myjoomla/views/myjoomla/tmpl/default.php
หลังจากสร้างไฟล์ และ โฟลเดอร์ เสร็จแล้ว เข้าระบบไปหน้า Admin และเข้าหน้า Discover ตามภาพ
- step01.jpg (55.36 KiB) Viewed 907 times
ในหน้า Discover ถ้าไม่เจอ component ของเราให้กดปุ่ม Discover ถ้าเจอ Component ของเราแล้วให้ ทำเครื่องหมายถูก แล้วกดปุ่ม Install
- step03.jpg (63.81 KiB) Viewed 907 times
ถ้าสำเร็จจะมี Component ของเราที่เมนู คลิกเข้าไป
- step02.jpg (56.97 KiB) Viewed 907 times
- step04.jpg (38.88 KiB) Viewed 907 times
ตัวอย่างไฟล์
:idea:
[size=150]ก่อนอื่นควรทำความเข้าใจกับ [url=https://www.mindphp.com/forums/viewtopic.php?f=29&t=11823]แนวคิด MVC กับการใช้งานใน joomla[/url] คลิกเข้าไปอ่านได้ครับ[/size]
[attachment=5]Joomla_mvc-diagram.png[/attachment]
[b][color=#008000]จากรูปจะขอสรุปส่วนการทำงานของ Components โดยแบ่งเป็น 3 ส่วน (ตามการเขียนโปรแกรมในรูปแบบ MVC) คือ
[u]ส่วนของ Controller[/u] จะเรียกใช้งานตาม task ที่มีการส่งข้อมูลเข้ามา ซึ่ง Controller จะเป็นตัวกำหนด Model และ View ที่จะใช้งาน
[u]ส่วนของ Model [/u]จะเป็นส่วนที่ติดต่อกับฐานข้อมูล การดึงข้อมูล เพิ่ม ลบ แก้ไข ทั้งหมดจะอยู่ภายในนี้ ในส่วน Controller และ View จะไม่มีการเรียกใช้ฐานข้อมูล
[u]ส่วนของ View[/u] เป็นส่วนแสดงผลไปยังผู้ใช้งาน ซึ่งใน View อาจจะมีได้หลาย Layout Controller จะสามารถกำหนด Layout ของ View ได้[/color][/b]
[b]สำหรับตัวอย่างโครงสร้าง Component ต่อไปนี้จะเป็น Component ทางฝั่ง Admin ครับ ลุย !!![/b]
เปิดโฟลเดอร์เข้าไปที่ administrator/components/ แล้วสร้างโฟลเดอร์ component ที่ต้องการ โดยการตั้งชื่อโฟลเดอร์ต้องขึ้นด้วย com_ เช่น ตั้งชื่อ hello ก็เป็น com_hello
[b]ตัวอย่างไฟล์โครงสร้าง[/b] สร้างไฟล์ และ โฟลเดอร์ ตามรายการด้านล่าง[code]com_myjoomla
- controllers
- helpers
- language
- - en-GB
- - - en-GB.com_myjoomla.ini
- - - en-GB.com_myjoomla.sys.ini
- models
- tables
- views
- - myjoomla
- - - tmpl
- - - - default.php
- - - view.html.php
- controller.php
- myjoomla.php
- myjoomla.xml[/code]
[b]ตัวอย่างโค้ด[/b]
- com_myjoomla/myjoomla.xml[code]<?xml version="1.0" encoding="utf-8"?>
<extension type="component" version="3.5" method="upgrade">
<name>com_myjoomla</name>
<author>Mindphp Developer Teams</author>
<creationDate>April 2016</creationDate>
<copyright>(C) 2005 - 2016 Open Source Matters. All rights reserved.</copyright>
<license>GNU General Public License version 2 or later; see LICENSE.txt</license>
<authorEmail>
[email protected]</authorEmail>
<authorUrl>www.mindphp.com</authorUrl>
<version>1.0.0</version>
<administration>
<menu>com_myjoomla</menu>
<files folder="admin">
<filename>controller.php</filename>
<filename>myjoomla.php</filename>
<folder>controllers</folder>
<folder>helpers</folder>
<folder>language</folder>
<folder>models</folder>
<folder>tables</folder>
<folder>views</folder>
</files>
</administration>
</extension>
[/code]
- com_myjoomla/myjoomla.php[code=php]defined('_JEXEC') or die;
JHtml::_('behavior.tabstate');
if (!JFactory::getUser()->authorise('core.manage', 'com_myjoomla'))
{
return JError::raiseWarning(404, JText::_('JERROR_ALERTNOAUTHOR'));
}
$controller = JControllerLegacy::getInstance('Myjoomla');
$controller->execute(JFactory::getApplication()->input->get('task'));
$controller->redirect();[/code]
- com_myjoomla/controller.php[code=php]defined('_JEXEC') or die;
class MyjoomlaController extends JControllerLegacy
{
} [/code]
- com_myjoomla/language/en-GB/en-GB.com_myjoomla.ini
- com_myjoomla/language/en-GB/en-GB.com_myjoomla.sys.ini[code]COM_MYJOOMLA = "My Joomla"[/code]
- com_myjoomla/views/myjoomla/view.html.php[code=php]defined('_JEXEC') or die;
class MyjoomlaViewMyjoomla extends JViewLegacy
{
public function display($tpl = null)
{
parent::display($tpl);
}
} [/code]
- com_myjoomla/views/myjoomla/tmpl/default.php
[code]My Joomla[/code]
หลังจากสร้างไฟล์ และ โฟลเดอร์ เสร็จแล้ว เข้าระบบไปหน้า Admin และเข้าหน้า Discover ตามภาพ[attachment=4]step01.jpg[/attachment]
ในหน้า Discover ถ้าไม่เจอ component ของเราให้กดปุ่ม Discover ถ้าเจอ Component ของเราแล้วให้ ทำเครื่องหมายถูก แล้วกดปุ่ม Install[attachment=3]step02.jpg[/attachment]
ถ้าสำเร็จจะมี Component ของเราที่เมนู คลิกเข้าไป[attachment=2]step03.jpg[/attachment][attachment=1]step04.jpg[/attachment]
ตัวอย่างไฟล์[attachment=0]com_myjoomla.zip[/attachment]