Joomla 3 คือระบบจัดการเนื้อหา (CMS) ที่ใช้สำหรับสร้างกับจัดการเว็บไซต์ และใน Joomla 3 Component คือส่วนขยายที่มีในตัวเอง ซึ่งเพิ่มฟังก์ชันการทำงานเฉพาะให้กับเว็บไซต์ ส่วนประกอบเป็นหนึ่งในสามประเภทของส่วนขยายใน Joomla 3 ส่วนประเภทอื่นๆ คือโมดูลและปลั๊กอิน หรือก็คือส่วนเสริมที่ใหญ่ที่สุดของ joomla ซึ่งใน โมดูล, ปลั๊กอิน และเทมเพลตไม่มีเพิ่มลบแก้ไข มีแต่ดึงข้อมูลมาแสดงโชว์ ถ้าเราจะทำเพิ่ม ลบ แก้ไขข้อมูล เราจะต้องสร้าง Component ซึ่งรูปแบบการเขียนจะเป็น OOP ในรูปแบบ MVC นั้นเอง
MVC (Model View Controller)
MVC ย่อมาจาก Model-View-Controller ซึ่งเป็นรูปแบบสถาปัตยกรรมซอฟต์แวร์ที่ใช้ในการพัฒนาเว็บแอปพลิเคชัน รูปแบบ MVC แยกแอปพลิเคชันออกเป็นสามส่วนที่เชื่อมต่อกัน ได้แก่ Model, View และ Controller
- Model แสดงถึงข้อมูลและคำนวณของแอปพลิเคชัน และโต้ตอบกับฐานข้อมูลหรือแหล่งข้อมูลอื่นๆ เพื่อดึงและจัดการข้อมูล
- View ทำหน้าที่ในการแสดงผลส่วนต่อกับผู้ใช้และนำเสนอข้อมูลแก่ผู้ใช้งาน
- Controller ทำหน้าที่เป็นตัวกลางระหว่าง Model และ View, ประมวลผลอินพุตของผู้ใช้, อัปเดต Model และเลือก View ที่เหมาะสมเพื่อแสดงผล
ด้วยการแยกแอปพลิเคชันออกเป็นสามองค์ประกอบ ของรูปแบบ MVC ส่งเสริมการแยกที่ชัดเจน และปรับปรุงความสามารถในการบำรุงรักษา และความสามารถในการทดสอบของแอปพลิเคชัน นอกจากนี้ยังช่วยให้หลาย Views ใช้โมเดลเดียวกันและอนุญาตให้ทำการเปลี่ยนแปลงกันได้โดยไม่ส่งผลกระทบต่อส่วนอื่นๆ
โฟลเดอร์และไฟล์ใน Component
Component ก็มีโฟล์เดอร์องค์ประกอบหลัก ก็คือ
- controllers
- View
- Model
Component ก็มีโฟลเดอร์เพิ่มเติม ก็คือ
- language ภาษา
- helpers เก็บฟังก์ชันที่เรียกใช้งานบ่อย ๆ เช่น api, javascript เป็นต้น
- layouts
- table เก็บ class ชื่อตารางของฐานข้อมูล
Component ก็มีไฟล์องค์ประกอบหลัก
- ไฟล์ xml ซึ่งไม่มี config ให้สร้างไฟล์แยก config.xml แทน กรณีที่จะทำ config
- ไฟล์ access.xml กำหนด user นั้นๆ ทำอะไรได้บ้าง
การสร้าง table เก็บข้อมูล
ในการสร้าง table เก็บข้อมูล ซึ่งใน Joomla 3 ตอนที่เราติดตั้ง Joomla 3 ก็มีช่วงติดตั้ง database มาให้แล้ว ซึ่งเราไม่ต้องมา database และ table มา เราแค่เพิ่ม table ที่เกี่ยวข้องกับพัฒนา component นั้นๆ เอง
ตัวอย่าง Table

ตัวอย่างการพัฒนา Component Joomla 3
ตัวอย่างการพัฒนา Component Joomla 3 ซึ่งเราจะเริ่มด้นด้วยสร้างโฟลเดอร์ในส่วนการทำงานของ admin ก่อนในการพัฒนา Component โดยอันดับแรกให้เราสร้างโฟลเดอร์ com_mexample ใน administrator และไปโฟลเดอร์ components

จากนั้นให้เริ่มสร้างไฟล์ mexample.xml เป็นส่วนแสดงรายละเอียดข้อมูลของ component com_mexample นั้นเอง

โค้ดในไฟล์ mexample.xml
<?xml version="1.0" encoding="utf-8"?>
<extension type="component" version="3.1" method="upgrade">
<name>com_mexample</name>
<author>Teams</author>
<creationDate>March 2023</creationDate>
<authorEmail>[email protected]</authorEmail>
<version>1.0.0</version>
<description>COM_MEXAMPLE_XML_DESCRIPTION</description>
<install>
<sql>
<file charset="utf8" driver="mysql">installation/sql/install.sql</file>
</sql>
</install>
<uninstall>
<sql>
<file charset="utf8" driver="mysql">installation/sql/uninstall.sql</file>
</sql>
</uninstall>
<files folder="site">
<filename>controller.php</filename>
<filename>router.php</filename>
<filename>mexample.php</filename>
<folder>controllers</folder>
<folder>language</folder>
<folder>models</folder>
<folder>views</folder>
</files>
<administration>
<menu>com_mexample</menu>
<files folder="admin">
<filename>config.xml</filename>
<filename>access.xml</filename>
<filename>controller.php</filename>
<filename>mexample.php</filename>
<folder>controllers</folder>
<folder>language</folder>
<folder>helpers</folder>
<folder>models</folder>
<folder>views</folder>
</files>
</administration>
</extension>
ซึ่งในโค้ดดังกล่าวจะเป็นการบอกรายละเอียดของ componet และใช้ไฟล์กับโฟลเดอร์อะไรบ้างใน component นั้นๆ
สร้างไฟล์ mexample.php เป็นไฟล์สำหรับเช็คสิทธิ์การใช้งาน โหลดหน้า และโหลดไฟล์ helpers หรือไฟล์อื่นๆ

โค้ดในไฟล์ mexample.php
<?php
/**
* @package com_mexample.Administrator
* @subpackage com_mexample
*/
defined('_JEXEC') or die;
JHtml::_('behavior.tabstate');
if (!JFactory::getUser()->authorise('core.manage', 'com_mexample'))
{
throw new JAccessExceptionNotallowed(JText::_('JERROR_ALERTNOAUTHOR'), 403);
}
JLoader::register('MExampleHelper', __DIR__ . '/helpers/mexample.php');
$controller = JControllerLegacy::getInstance('MExample');
$controller->execute(JFactory::getApplication()->input->get('task'));
$controller->redirect();
สร้างไฟล์ controller.php ซึ่งเป็นไฟล์ที่เกี่ยวกับการจัดการในส่วนหลักของ component นั้นๆ

โค้ดในไฟล์ controller.php
<?php
/**
* @package com_mexample.Administrator
* @subpackage com_mexample
*/
defined('_JEXEC') or die;
class MexampleController extends Joomla\CMS\MVC\Controller\BaseController
{
}
สร้างไฟล์ config.xml คือไฟล์กำหนดค่า config ต่างๆ ใน component

โค้ดในไฟล์ config.xml
<?xml version="1.0" encoding="utf-8"?>
<config>
<fieldset
name="permissions"
label="JCONFIG_PERMISSIONS_LABEL"
description="JCONFIG_PERMISSIONS_DESC"
>
<field
name="rules"
type="rules"
label="JCONFIG_PERMISSIONS_LABEL"
filter="rules"
validate="rules"
component="com_mexample"
section="component"
/>
</fieldset>
</config>
สร้างไฟล์ access.xml คือไฟล์กำหนดสิทธิ์การใช้งานของ user

โค้ดในไฟล์ access.xml
<?xml version="1.0" encoding="utf-8" ?>
<access component="com_mexample">
<section name="component">
<action name="core.admin" title="JACTION_ADMIN" description="JACTION_ADMIN_COMPONENT_DESC" />
<action name="core.options" title="JACTION_OPTIONS" description="JACTION_OPTIONS_COMPONENT_DESC" />
<action name="core.manage" title="JACTION_MANAGE" description="JACTION_MANAGE_COMPONENT_DESC" />
<action name="core.create" title="JACTION_CREATE" description="JACTION_CREATE_COMPONENT_DESC" />
<action name="core.delete" title="JACTION_DELETE" description="JACTION_DELETE_COMPONENT_DESC" />
<action name="core.edit" title="JACTION_EDIT" description="JACTION_EDIT_COMPONENT_DESC" />
<action name="core.edit.state" title="JACTION_EDITSTATE" description="JACTION_EDITSTATE_COMPONENT_DESC" />
<action name="core.edit.value" title="JACTION_EDITVALUE" description="JACTION_EDITVALUE_COMPONENT_DESC" />
</section>
</access>
เบื้องต้นก็คือ การพัฒนา component Joomla หลักๆ ในการพัฒนาก็คือ MVC เช่น Model เป็นส่วนของการตคำนวณกับฐานข้อมูล, View เป็นส่วนของการแสดงผลเว็บไซต์หน้าตาของ component และสุดท้าย controller ควบคุมระหว่าง Model และ View นั้นเอง ซึ่งทั้ง 3 ส่วนนี้ก็จะเป็นโฟลเดอร์หลักในการพัฒนา Component และยังมีโฟลเดอร์อื่นๆ ซึ่งมาช่วยพัฒนาได้ ยกตัวอย่างเช่น helpers, tables, assets, language และโฟลเดอร์อื่นๆ
อ้างอิง
วิธีสร้าง Component Joomla แบบง่าย ,[ออนไลน์], เข้าถึงได้จาก https://www.mindphp.com/forums/viewtopic.php?p=238674#p238674
Component ,[ออนไลน์], เข้าถึงได้จาก https://docs.joomla.org/Component
Step-by-step Guide For Creating Joomla 3.x Component ,[ออนไลน์], เข้าถึงได้จาก https://www.techfry.com/joomla/step-by-step-guide-for-creating-joomla-3-x-component