แนะนำโครงสร้าง Component ของ Joomla [3.x]

แชร์ ความรู้ในการ พัฒนา Joomla Component Extension Module Plugin

Moderator: mindphp, ผู้ดูแลกระดาน

ภาพประจำตัวสมาชิก
tsukasaz
PHP VIP Members
PHP VIP Members
โพสต์: 21991
ลงทะเบียนเมื่อ: 18/04/2012 9:39 am

แนะนำโครงสร้าง Component ของ Joomla [3.x]

โพสต์ที่ยังไม่ได้อ่าน โดย tsukasaz »

:idea:
ก่อนอื่นควรทำความเข้าใจกับ แนวคิด MVC กับการใช้งานใน joomla คลิกเข้าไปอ่านได้ครับ
Joomla_mvc-diagram.png
Joomla_mvc-diagram.png (37.45 KiB) Viewed 904 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 = "My Joomla"
- 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
step01.jpg (55.36 KiB) Viewed 904 times
ในหน้า Discover ถ้าไม่เจอ component ของเราให้กดปุ่ม Discover ถ้าเจอ Component ของเราแล้วให้ ทำเครื่องหมายถูก แล้วกดปุ่ม Install
step02.jpg
step02.jpg (56.97 KiB) Viewed 904 times
ถ้าสำเร็จจะมี Component ของเราที่เมนู คลิกเข้าไป
step03.jpg
step03.jpg (63.81 KiB) Viewed 904 times
step04.jpg
step04.jpg (38.88 KiB) Viewed 904 times
ตัวอย่างไฟล์
com_myjoomla.zip
(4.58 KiB) ดาวน์โหลดแล้ว 144 ครั้ง
The last bug isn't fixed until the last user is dead. (Sidney Markowitz, 1995)

ผู้ใช้งานขณะนี้

สมาชิกกำลังดูบอร์ดนี้: ไม่มีสมาชิกใหม่ และบุคลทั่วไป 63