สร้าง Component สำหรับ Joomla 2.5 การสร้างส่วน view

ตอบกระทู้

รูปแสดงอารมณ์
:icon_plusone: :like: :plusone: :gfb: :-D :) :( :-o 8O :? 8) :lol: :x :P :oops: :cry: :evil: :twisted: :roll: :wink: :!: :?: :idea: :arrow: :| :mrgreen: :angry: :baa: :biggrin:
รูปแสดงอารมณ์อื่นๆ

BBCode เปิด
[img] เปิด
[url] เปิด
[Smile icon] เปิด

กระทู้แนะนำ
   

มุมมองที่ขยายได้ กระทู้แนะนำ: สร้าง Component สำหรับ Joomla 2.5 การสร้างส่วน view

Re: สร้าง Component สำหรับ Joomla 2.5 การสร้างส่วน view

โดย gain9999 » 08/10/2012 5:17 pm

Tip: ปัญหาแบ่งหน้าแล้วมีปัญหา stat ไม่จำค่า แก้ด้วย ตั้งชื่อตามรูปแบบตัวอย่าง

สร้าง Component สำหรับ Joomla 2.5 การสร้างส่วน view

โดย tsukasaz » 08/05/2012 1:05 pm

ส่วน view คืออะไร ถ้าไม่เข้าใจศึกษาแนวคิดนะครับ
แนวคิด MVC Joomla

ก่อนอื่นต้องมีไฟล์ตามขั้นตอนนี้มาก่อน สร้าง Component สำหรับ Joomla 2.5 แบบพื้นฐาน

1. ไปที่โฟลเดอร์ helloworld ที่เราสร้างไว้ในหัวข้อที่แ้ล้ว เข้าไปที่ site -> 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();
?>
2. ในโฟลเดอร์ site ให้สร้างไฟล์ controller.php แล้วใส่โค้ด

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

<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
 
// import Joomla controller library
jimport('joomla.application.component.controller');
 
/**
 * Hello World Component Controller
 */
class HelloWorldController extends JController
{
}
?>
3. สร้างโฟลเดอร์ views เปิดเข้าไป สร้างโฟลเดอร์ชื่อ helloworld เปิดเข้าไป สร้างไฟล์ 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');
 
/**
 * HTML View class for the HelloWorld Component
 */
class HelloWorldViewHelloWorld extends JView
{
    // Overwriting JView display method
    function display($tpl = null) 
    {
        // Assign data to the view
        $this->msg = 'Hello World';
 
        // Display the view
        parent::display($tpl);
    }
}
?>
4. ให้สร้างโฟลเดอร์ tmpl เปิดเข้าไป สร้างไฟล์ default.php ใส่โค้ด

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

<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
?>
<h1><?php echo $this->msg; ?></h1>
?>
5. เปิดไฟล์ helloworld.xml ขึ้นมาแก้

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

<version>0.0.1</version>
เปลี่ยนเป็น

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

<version>0.0.2</version>
และ

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

<files folder="site">
		<filename>index.html</filename>
		<filename>helloworld.php</filename>
	</files>
เปลี่ยนเป็น

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

<files folder="site">
		<filename>index.html</filename>
		<filename>helloworld.php</filename>
		<filename>controller.php</filename>
		<folder>views</folder>
	</files>

6. ทำเป็น .zip แล้ว เข้าไปหน้าของ administrator ไปเพิ่ม extensions เ้ข้าไปใหม่

รันโค้ดโดยพิม URL = index.php?option=com_helloworld
แนบไฟล์
view.JPG
view.JPG (26.69 KiB) Viewed 3799 times

ข้างบน