สร้าง Component สำหรับ Joomla 2.5 พื้นฐานส่วนของ backend

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

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

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

สร้าง Component สำหรับ Joomla 2.5 พื้นฐานส่วนของ backend

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

ตอนนี้เราจะลองมาทำหน้าแสดงผลในส่วนของ administrator กันบ้างนะครับ

เริ่มจากเข้าไปที่โฟลเดอร์ admin เปิดไฟล์ helloworld.php ขึ้นมาแก้นะครับ

โค้ด: เลือกทั้งหมด

<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
 
// import joomla controller library
jimport('joomla.application.component.controller');
 
// Get an instance of the controller prefixed by HelloWorld
$controller = JController::getInstance('HelloWorld');
 
// Perform the Request task
$controller->execute(JRequest::getCmd('task'));
 
// Redirect if set by the controller
$controller->redirect();
?>
แล้วสร้างไฟล์ controller.php ขึ้นมา

โค้ด: เลือกทั้งหมด

<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
 
// import Joomla controller library
jimport('joomla.application.component.controller');
 
/**
 * General Controller of HelloWorld component
 */
class HelloWorldController extends JController
{
	/**
	 * display task
	 *
	 * @return void
	 */
	function display($cachable = false) 
	{
		// set default view if not set
		JRequest::setVar('view', JRequest::getCmd('view', 'HelloWorlds'));
 
		// call parent behavior
		parent::display($cachable);
	}
}
?>
เริ่มทำส่วน view กัน โดยสร้างโฟลเดอร์ views เปิดเข้าไป สร้างโฟลเดอร์ helloworlds สร้างไฟล์ view.html.php

โค้ด: เลือกทั้งหมด

<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
 
// import Joomla view library
jimport('joomla.application.component.view');
 
/**
 * HelloWorlds View
 */
class HelloWorldViewHelloWorlds extends JView
{
	/**
	 * HelloWorlds view display method
	 * @return void
	 */
	function display($tpl = null) 
	{
		// Get data from the model
		$items = $this->get('Items');
		$pagination = $this->get('Pagination');
 
		// Check for errors.
		if (count($errors = $this->get('Errors'))) 
		{
			JError::raiseError(500, implode('<br />', $errors));
			return false;
		}
		// Assign data to the view
		$this->items = $items;
		$this->pagination = $pagination;
 
		// Display the template
		parent::display($tpl);
	}
}
?>
เสร็จแล้วสร้างโฟลเดอร์ tmpl แล้วสร้างไฟล์ default.php

โค้ด: เลือกทั้งหมด

<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted Access');
 
// load tooltip behavior
JHtml::_('behavior.tooltip');
?>
<form action="<?php echo JRoute::_('index.php?option=com_helloworld'); ?>" method="post" name="adminForm" id="adminForm">
	<table class="adminlist">
		<thead><?php echo $this->loadTemplate('head');?></thead>
		<tfoot><?php echo $this->loadTemplate('foot');?></tfoot>
		<tbody><?php echo $this->loadTemplate('body');?></tbody>
	</table>
</form>
สร้างไฟล์ default_head.php

โค้ด: เลือกทั้งหมด

<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted Access');
?>
<tr>
	<th width="5">
		<?php echo JText::_('COM_HELLOWORLD_HELLOWORLD_HEADING_ID'); ?>
	</th>
	<th width="20">
		<input type="checkbox" name="toggle" value="" onclick="checkAll(<?php echo count($this->items); ?>);" />
	</th>			
	<th>
		<?php echo JText::_('COM_HELLOWORLD_HELLOWORLD_HEADING_GREETING'); ?>
	</th>
</tr>
สร้างไฟล์ default_foot.php

โค้ด: เลือกทั้งหมด

<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted Access');
?>
<?php foreach($this->items as $i => $item): ?>
	<tr class="row<?php echo $i % 2; ?>">
		<td>
			<?php echo $item->id; ?>
		</td>
		<td>
			<?php echo JHtml::_('grid.id', $i, $item->id); ?>
		</td>
		<td>
			<?php echo $item->greeting; ?>
		</td>
	</tr>
<?php endforeach; ?>
แล้วกลับไปที่โฟลเดอร์ admin -> models สร้างไฟล์ helloworlds.php ขึ้นมานะครับ

โค้ด: เลือกทั้งหมด

<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
// import the Joomla modellist library
jimport('joomla.application.component.modellist');
/**
 * HelloWorldList Model
 */
class HelloWorldModelHelloWorlds extends JModelList
{
	/**
	 * Method to build an SQL query to load the list data.
	 *
	 * @return	string	An SQL query
	 */
	protected function getListQuery()
	{
		// Create a new query object.		
		$db = JFactory::getDBO();
		$query = $db->getQuery(true);
		// Select some fields
		$query->select('id,greeting');
		// From the hello table
		$query->from('#__helloworld');
		return $query;
	}
}
?>
เตรียมไฟล์เสร็จหมดแล้ว เปิดไฟล์ helloworld.xml ขึ้นมาแก้หน่อยครับ

โค้ด: เลือกทั้งหมด

<files folder="admin">
         <!-- Admin Main File Copy Section -->
         <filename>index.html</filename>
         <filename>helloworld.php</filename>
         <!-- SQL files section -->
         <folder>sql</folder>
         <!-- tables files section -->
         <folder>tables</folder>
         <!-- models files section -->
         <folder>models</folder>
      </files>
แก้เป็น

โค้ด: เลือกทั้งหมด

<files folder="admin">
         <!-- Admin Main File Copy Section -->
         <filename>index.html</filename>
         <filename>helloworld.php</filename>
         <!-- SQL files section -->
         <folder>sql</folder>
         <!-- tables files section -->
         <folder>tables</folder>
         <!-- models files section -->
         <folder>models</folder>
         <folder>views</folder>
      </files>
แก้เวอร์ชั่น จาก 0.0.6 เป็น 0.0.7 นะครับ

ทำเป็น .zip แล้ว เข้าไปหน้าของ administrator ไปเพิ่ม extensions เข้าไปใหม่
The last bug isn't fixed until the last user is dead. (Sidney Markowitz, 1995)
ภาพประจำตัวสมาชิก
tsukasaz
PHP VIP Members
PHP VIP Members
โพสต์: 22164
ลงทะเบียนเมื่อ: 18/04/2012 9:39 am

Re: สร้าง Component สำหรับ Joomla 2.5 พื้นฐานส่วนของ backend

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

เข้าไปในหน้าของ administrator เลือกเมนู Components -> hello-world
แนบไฟล์
backend.JPG
backend.JPG (47.43 KiB) Viewed 3513 times
The last bug isn't fixed until the last user is dead. (Sidney Markowitz, 1995)
ภาพประจำตัวสมาชิก
tsukasaz
PHP VIP Members
PHP VIP Members
โพสต์: 22164
ลงทะเบียนเมื่อ: 18/04/2012 9:39 am

Re: สร้าง Component สำหรับ Joomla 2.5 พื้นฐานส่วนของ backend

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

จะได้การแสดงผล
แนบไฟล์
backend2.JPG
backend2.JPG (26.48 KiB) Viewed 3512 times
The last bug isn't fixed until the last user is dead. (Sidney Markowitz, 1995)
rissydang
PHP Newbie
PHP Newbie
โพสต์: 1
ลงทะเบียนเมื่อ: 12/02/2015 2:21 pm

Re: สร้าง Component สำหรับ Joomla 2.5 พื้นฐานส่วนของ backend

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

มันขั้น error นี้ค่ะ
500 - An error has occurred.
Invalid controller: name='', format=''
ตอบกลับโพส

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

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