หน้า 1 จากทั้งหมด 1

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

โพสต์แล้ว: 11/05/2012 10:37 am
โดย 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 เข้าไปใหม่

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

โพสต์แล้ว: 11/05/2012 10:41 am
โดย tsukasaz
เข้าไปในหน้าของ administrator เลือกเมนู Components -> hello-world

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

โพสต์แล้ว: 11/05/2012 10:43 am
โดย tsukasaz
จะได้การแสดงผล

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

โพสต์แล้ว: 15/02/2015 9:31 pm
โดย rissydang
มันขั้น error นี้ค่ะ
500 - An error has occurred.
Invalid controller: name='', format=''