MVC

MVC ဆိုတာ Model View Controller ဆိုတာ လူတိုင်း သိနေလောက်ပါပြီ။ သို့ပေမယ့် တော်တော်များများက MVC နဲ့ framework ဆက်စပ်နေတယ်လို့ ထင်ကြပါတယ်။ CodeIgniter က MVC သုံးထားတယ်။ Symfony လည်း MVC သုံးထားတာပဲ။ CakePHP လည်း MVC ပဲ။ ဒါပေမယ့် သုံးပုံ သုံးနည်း မတူကြဘူး။

MVC ဆိုတာ တကယ်တန်းတော့ framework နဲ့ သက်ဆိုင်တာ မဟုတ်တဲ့ architectural pattern တစ်ခုပါ။ အခုတလော ExpressJs ကို သုံးပြီး Mongodb နဲ့ ရေးနေရင်းနဲ့ MVC pattern အကြောင်း သဘောပေါက်လာတယ်။ ပုံမှန် သမာရိုးကျ Web app တွေမှာ controller ရဲ့ အရေးပါပုံ မသိသာ ပေမယ့် routing address တွေ များလာတဲ့ အခါမှာ controller နဲ့ model ချိတ်ဆက်တာ အရမ်းအရေးပါလာတယ်။ သာမာန် Web App တွေမှာ GET နဲ့ POST ကိုပဲ အဓိကသုံးပေမယ့် RESTful မှာတော့ GET , POST , PUT , DELETE ကို အသုံးပြုကြပါတယ်။ Controller အနေနဲ့ GET နဲ့ ဒီ လိပ်စာလာရင် ဒီ model ကို ခေါ်ပြီး ဒါကို လုပ်။ POST ဆိုရင်တော့ ဒါကို လုပ်ဆိုပြီး ခွဲခြမ်း စိတ်ဖြာပေးပြီး ရလာတဲ့ result တွေကို View နဲ့ ပြန်ထုတ်ပြပါတယ်။

MVC က ဘယ် framework မှ မပါပဲ PHP သီးသန့်မှာ ကိုယ့်ဘာသာကိုယ် MVC ပုံစံ ရေးသားလို့ရပါတယ်။ MVC pattern ပုံစံကို Node.js မှာ ပိုထင်ရှားစေပါတယ်။


var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/html; charset=UTF-8'});
  res.end('Hello World');
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');

အထက်ပါ code သမာရိုးကျ Node.js code ပါ။ http://127.0.0.1:1337/ ကိုပဲ ခေါ်ခေါ် http://127.0.0.1:1337/something ပဲ ခေါ်ခေါ် Hello World ကို bold နဲ့ လုပ်ထားတာပဲ ပေါ်ပါလိမ့်မယ်။

ဒီတော့ ကျွန်တော်တို့တွေ routing ပိုင်းတွေ ရေးဖို့ လိုလာပါပြီ။

routing ပိုင်းတွေနဲ့ အဓိက coding ပိုင်းတွေ ရေးတဲ့ အခါမှာ code တွေက ရှုပ်ကုန်ပါလိမ့်မယ်။ ဒါကြောင့် သီးသန့် ခွဲထုတ်ပြီး ရေးသားပါတယ်။ Node.js မှာတော့ Module ရေးတယ်လို့ ဆိုနိုင်ပါတယ်။ Router နဲ့ Module ကောင်းစွာ သီးသန့် ရေးပြီးသွားရင် Node.js မှာ HTML ကို ပြဖို့ res.end ဆိုပြီး ရေးခဲ့တာ တွေ့နိုင်ပါတယ်။ HTML code အများကြီးဆိုရင်တော့ မလွယ်ကူလှပါဘူး။ ဒါကြောင့် template engine တစ်ခုခု ကို သုံးပြီး template နဲ့ ရေးဖို့ လိုအပ်ပါတယ်။ ကျွန်တော်ကတော့ ExpressJS မှာ jade ကို အသုံးပြုပါတယ်။

ဒီအဆင့်တွေကို ကြည့်လိုက်ရင် MVC ဆိုတာ ဘာလဲဆိုတာကို သိသိသာသာ မြင်နိုင်ပါတယ်။ PHP ကို ဘာ framework မှ မပါပဲ ကိုယ့်ဘာသာကိုယ် MVC pattern အနေနဲ့ ရေးလို့ရပါတယ်။ web ပိုင်းမှာမှ မဟုတ်ပဲ Desktop Application တွေမှာလည်း MVC ကို အသုံးပြုနိုင်ပါတယ်။ iOS development မှာလည်း MVC ကို အသုံးပြုနိုင်ပါတယ်။ MVC ဆိုတာ တကယ်တော့ ရိုးရိုးရှင်းရှင်းလေးပါပဲ။ သို့ပေမယ့် ဖန်တီးသူရဲ့ အတွေးပေါ်မှာ မူတည်ပြီး တစ်ခုနဲ့ တစ်ခု pattern တည်ဆောက်ပုံတွေ အနည်းငယ် ကွဲပြားသွားတာပါ။

3 Comments

  1. ူောိသအ says:

    When opening this web page useless image filled up my screen, I can view text only when I scrolled it down and still 50 % of web page is blank.It looks like unable to tackle space-wasting.

  2. Min Maung says:

    စေတန်ဂေါ့ကိုတိုင် အတိတ်တုန်းက ကိုယ်ပိုင် MVC Framework ကိုတီထွင်ခဲ့ဖူးတယ်လို့ပြောခဲ့ဘူးတယ်။အခုတော့နားလည်သွားပြီထင်ပါတယ်။
    MVC ဆိုတာ Framework မှမဟုတ်တာPattern တစ်ခုဘဲ။

    1. saturngod says:

      Yes, I created Ava PHP framework base on MVC pattern. MVC framework mean framework base on MVC pattern.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.