Google




ความคืบหน้า PHP Hoffman Framework (5)

จาก ความคืบหน้า PHP Hoffman Framework (4) ตัวความสัมพันธ์ระหว่าง model กับ controller นั้น เพิ่มเติมและปรับเปลี่ยนเพื่อความเหมาะสม โดยถ้าเราจะกำหนด controller ต้องมี namespace หรือ prefix-name (ต่อไปจะเรียกว่า namespace อย่างเดียวครับ) ด้านหน้าเสียก่อน คือ

PHP:
  1. class controller_<ชื่อ controller> extends Hmf_FlowController {
  2.     ....
  3. }

โดยให้ extends มาจาก FlowController ครับ

ส่วนของ model ก็เช่นกันครับ

PHP:
  1. class model_<ชื่อ model> extends Hmf_LogicModel {
  2.     function __construct(){
  3.         parent::__construct();
  4.     }
  5.     ....
  6. }

(อันนี้คือ model แบบมาตรฐาน)

แต่เวลาเรียกเอา model มาใช้ใน FlowController ก็ใช้ method 'getInstantModel' ของ FlowController ครับ โดยใส่ชื่อ model ลงไป และเดี่ยวมันจะเติม namespace ให้เอง แบบนี้ครับ

PHP:
  1. class model_user extends Hmf_LogicModel {
  2.     function __construct(){
  3.         parent::__construct();
  4.     }
  5.     ....
  6. }
  7.  
  8. class controller_user extends Hmf_FlowController {
  9.     function list($page = 1){
  10.        
  11.         $user = $this->getInstantModel('user');
  12.        
  13.         $out['data'] = $user->pagination($page, 5);           
  14.         $out['pagenav'] = $user->paginationLink(HttpPage::url('user','list'));
  15.        
  16.         return $out;
  17.        
  18.     }
  19. }

แต่ถ้า model_user ไม่มี หรือไม่ได้สร้าง class ไว้ ตัว getInstantModel มันจะไปสร้าง Instant ของ class ของ model นั้นแล้วให้ตัวมันมีคุณสมบัติแบบเดียวกับ class ของ model แบบมาตรฐานด้านบนนั้นแหละครับ พูดง่าย ๆ ก็คือถ้าหาไม่เจอ ก็สร้างให้เลย ส่วนใช้งานได้หรือไม่อีกเรื่องครับ โดย class ของ model แบบมาตรฐานมีคุณสมบติแบบเดียวกับ LogicModel เลยครับ

PHP:
  1. class controller_user extends Hmf_FlowController {
  2.     function list($page = 1){
  3.        
  4.         $user = $this->getInstantModel('user');
  5.        
  6.         $out['data'] = $user->pagination($page, 5);           
  7.         $out['pagenav'] = $user->paginationLink(HttpPage::url('user','list'));
  8.        
  9.         return $out;
  10.        
  11.     }
  12. }

โดยตัวอย่างด้านบน ถ้าไม่มี model_user ก็ใช้งานได้เหมือนกันครับ ซึ่งสะดวกมากในกรณีที่ใช้ class มาตรฐานทั่วไปครับผม

Related Posts


2 Responses to “ความคืบหน้า PHP Hoffman Framework (5)”


  1. 1 pphetra

    ดีขึ้น, post ก่อนผมเห็น ประโยค

    $albumlist = new albumlist();

    แล้วรู้สึกทะแม่งๆ, ตอนนี้เปลี่ยนเป็น getInstantModel แล้วดูดีขึ้น

  2. 2 Ford AntiTrust

    pphetra - ขอบคุณครับ ตอนนี้ผมกำลังไล่ทำพวก class และ pattern พื้นฐานที่จำเป็นทั้งหมดก่อน ท่าทางงานนี้จะ late ไปกว่าที่ควรจะเป็นครับผม

Leave a Reply

This is a captcha-picture. It is used to prevent mass-access by robots. (see: www.captcha.net)

You must read and type the 5 chars within 0..9 and A..F, and submit the form.

  

Oh no, I cannot read this. Please, generate a






Close
E-mail It