Growing Joomla! Community is Our Goal.
พุธ 10 มีนาคม 2553
   
Text Size

การเขียน Joomla! 1.0.x component ตอนที่ 1

( 4 Votes )

Joomla! component เป็นส่วนขยายของ Joomla! ที่มีการใช้พื้นที่หลักของ web site ในการแสดงผลต่างจาก modules ที่ใช้พื้นที่ที่กำหนดใน template สำหรับการแสดงผล ซึ่งเรียกว่า module position โดยใน version 1.0.x นี้การเขียน component ไม่มีความซับซ้อนมากนัก (ในแง่ของข้อกำหนดและ concept) จึงเหมาะสำหรับมือใหม่มากกว่าการเริ่มต้นกับ Joomla! 1.5.x โดยการเรียกใช้ component จะเป็นการเรียกผ่าน menu ซึ่งกำหนดโดยผู้ดแล web site เป็นหลัก โดย Joomla! core จะทำการเรียกโดยการ include ไฟล์หลักของ component ตามตัวแปรชื่อ $option เช่น index.php?option=com_mymenu จะทำการตรวจสอบไฟล์จาก folder ชื่อ components/com_mymenu และ include ไฟล์ mymenu.php

สำหรับบทความนี้จะทำให้ผู้อ่านเข้าใจขั้นตอนของการเขียน Joomla! 1.0.x component การสร้าง zip ไฟล์สำหรับการติดตั้ง การใช้งาน Joomla! database object เบื้องต้น

  1. โครงสร้างของ folder ทั้ง frontend และ backend
  2. การสร้างไฟล์ installation
  3. โครงสร้างของ j15_components
  4. การสร้าง toolbar (backend) 
  5. โปรแกรมทางด้าน backend

โครงสร้างของ folder ทั้ง frontend และ backend

โครงของ folder และการตั้งชื่อ component ถือว่าสำคัญมากครับ โดยใน frontend เราจะมี folder ชื่อ /components อยู่แล้ว หาก component ของเราชื่อ mycomponent ดังนั้นเราจะใส่ไฟล์ frontend ไว้ที่ /components/com_mycomponent/ โดยเราจะมีไฟล์อยู่สามไฟล์หลักๆ ในนี้คือ

/components/com_mycomponent/mycomponent.php 
/components/com_mycomponent/mycomponent.html.php
/components/com_mycomponent/mycomponent.class.php

โดยในไฟล์แรก ถือว่าเป็นไฟล์บังคับเพราะเป็นไฟล์ที่ core จะหาจาก $option และ include เข้าไป สำหรับไฟล์ที่สอง เรียกว่า front_html เป็นไฟล์ที่จะมี HTML สำหรับการแสดงผลโดยเฉพาะ ส่วนไฟล์ที่สามเรียกว่า class เป็นส่วนของ Business Logic หรือ Model ตาม concept ของ Model-View-Controller (MVC)  โดยเราสามารถอ้างถึงไฟล์สองไฟล์ได้โดยอาศัย method ของ $mainframe object ซึ่งเป็นตัวแปร global ที่สร้างโดย core นะครับ ดังนี้
$mainframe and file include
  1. require ( $mainframe->getPath('front_html') );//absolute path to mycomponent.html.php
  2. require ( $mainframe->getPath('class') ); //absolute path to mycomponent.class.php
สำหรับในส่วนของ backend ก็ใช้วิธีการแบบเดียวกันครับ แต่ใน backend จะไม่มี class คิดว่าเพราะทางทีมอยากให้มี class แค่ชุดเดียวอยู่ใน frontend เลย เรามาดูไฟล์กันเลยนะครับสำหรับ backend
/administrator/components/com_mycomponent/admin.mycomponent.php 
/administrator/components/com_mycomponent/admin.mycomponent.html.php
/administrator/components/com_mycomponent/toolbar.mycomponent.php
/administrator/components/com_mycomponent/toolbar.mycomponent.html.php

โดยใน folder /administrator/components/com_mycomponent นี้ก็คล้ายกับ frontend ครับแต่จะมีไฟล์สำหรับการสร้าง TOOLBAR เพิ่มเข้ามา
JoomlaWatch Stats 1.2.8b_12-dev by Matej Koval
Restore Default Settings