PHP သည် 0 က်ဘ်အပလီကေးရှင်းများဖွံ့ဖြိုးတိုးတက်ရေးတွင်ကျယ်ကျယ်ပြန့်ပြန့်အသုံးပြုသောလူကြိုက်များသော server-side scripting language ဖြစ်သည်။ PHP framework တစ်ခုတည်ဆောက်ခြင်းသင်ကိုယ်တိုင်သည်ပိုမိုကောင်းမွန်သောစီမံခန့်ခွဲမှုများကိုပိုမိုကောင်းမွန်စွာစီမံခန့်ခွဲနိုင်ပြီးသင်၏ကုဒ်၏ပြန်လည်သုံးသပ်ခြင်းနှင့်ပမာဏကိုတိုးတက်စေရန်ကူညီနိုင်သည်။ ဤဆောင်းပါးသည်အခြေခံ PHP framework ကို scratch မှမည်သို့တည်ဆောက်ရမည်ကိုပြသရန်နှင့်သက်ဆိုင်ရာကုဒ်များဥပမာများကိုပေးရမည်။
ပထမ ဦး စွာမူဘောင်ဖိုင်များကိုသိမ်းဆည်းထားသောလမ်းညွှန်တစ်ခုကိုသင်ဖန်တီးရန်လိုအပ်သည်။ ဤလမ်းညွှန်ကိုသင်၏ကြိုက်နှစ်သက်ရာများနှင့်အညီအမည်ပေးနိုင်သည်။ ဤလမ်းညွှန်တွင်အောက်ပါဖွဲ့စည်းပုံကိုကျွန်ုပ်တို့ဖန်တီးပါမည်။
myframework
├── app
│ ├── controllers
│ ├── models
│ └── views
├── config
├── public
├── vendor
└── index.php
ဤဖွဲ့စည်းပုံတွင်အက်ပလီကေးရှင်းလမ်းညွှန်သည် Controller များ, မော်ဒယ်များနှင့်အမြင်များအပါအ 0 င် application ၏ကုဒ်ကိုသိမ်းဆည်းရန်အသုံးပြုသည်။ config directory ကို configuration files များအတွက်အသုံးပြုသည်။ အများပြည်သူလမ်းညွှန်ကိုအများပြည်သူဆိုင်ရာအရင်းအမြစ်ဖိုင်များ (ဥပမာ CSS နှင့် JavaScript) အတွက်အသုံးပြုသည်။ ရောင်းချသူလမ်းညွှန်ကို Third-party libraries များတွင်အသုံးပြုသည်။ နှင့် index.php သည်မူဘောင်၏ entry file ဖြစ်သည်။
ထို့နောက် app directory တွင် framework.php အမည်ရှိဖိုင်တစ်ခုကိုဖန်တီးပါဘောင်၏အခြေခံလူတန်းစားအဖြစ်ဖန်တီးပါ။ ဤအတန်း၏လုပ်ငန်းတာဝန်မှာတောင်းဆိုမှုများ, routing နှင့် schedolling controller များကိုကိုင်တွယ်ရန်ဖြစ်သည်။
class Framework {
public function __construct() {
// မူဘောင်ကိုအစပြုပါ
}
public function run() {
// လုပ်ငန်းစဉ်တောင်းဆိုမှုများနှင့်လမ်းကြောင်း
$route = $this->getRoute();
$controller = $this->getController($route['controller']);
$action = $route['action'];
if (method_exists($controller, $action)) {
$controller->$action();
} else {
// မတည်ရှိသောနည်းလမ်းများ
}
}
private function getRoute() {
// resolution တောင်းဆိုမှုURL,Controller နှင့်နည်းလမ်းရယူပါ
$route = $_SERVER['REQUEST_URI'];
return ['controller' => $controller, 'action' => $action];
}
private function getController($controllerName) {
// Controller အမည်ပေါ် အခြေခံ. Controller ဥပမာတစ်ခုဖန်တီးပါ
$controllerClass = $controllerName . 'Controller';
$controllerFile = 'app/controllers/' . $controllerClass . '.php';
if (file_exists($controllerFile)) {
require_once $controllerFile;
return new $controllerClass;
} else {
// non- တည်ရှိ controllers ကိုင်တွယ်
}
}
}
အထက်ပါကုဒ်တွင် `__Construct ()` နည်းလမ်းကို `method ကိုမူဘောင်ကိုစတင်ရန်အစပြုရန်အသုံးပြုသည်။ `getroute ()` သည် CONCUNTROLLER () `contontroller () သည် Controller အမည်ပေါ် မူတည်. Controller ဥပမာအားဖန်တီးရန်တာဝန်ရှိသည်။
ထို့နောက်ကျွန်ုပ်တို့သည်အက်ပလီကေးရှင်းများ / Controllers လမ်းညွှန်တွင်ဥပမာ controller တစ်ခုအနေဖြင့်,
class ExampleController {
public function index() {
// Home Page Land ကိုကိုင်တွယ်ပါ
}
public function about() {
// စာမျက်နှာ Logic အကြောင်းကိုင်တွယ်ပါ
}
// အခြားနည်းလမ်းများ...
}
In this code, we create a class called ExampleController and define two methods `index()` and `about()` as examples.
အက်ပလီကေးရှင်း / အမြင်များလမ်းညွှန်တွင်နမူနာမြင်ကွင်းအဖြစ် Index.php အမည်ရှိဖိုင်တစ်ခုကိုဖန်တီးပါ။ ဤအမြင်တွင်စာမျက်နှာအကြောင်းအရာကိုထုတ်လုပ်ရန် HTML နှင့် PHP ကိုရေးနိုင်သည်။
<!DOCTYPE html>
<html>
<head>
<title>My Framework</title>
</head>
<body>
<h1>Welcome to my framework!</h1>
</body>
</html>
အထက်ပါကုဒ်သည်မူဘောင်၏ရှေ့တန်းစာမျက်နှာအဖြစ်ဆောင်ရွက်မည့်ရိုးရှင်းသော HTML View ကိုပြသသည်။
Index.php အမည်ရှိ index.php အမည်ရှိဖိုင်တစ်ခုကိုဖန်တီးပါဘောင်နေရာ၏ root directory ထဲတွင်ပြုလုပ်ပါ။ ထိုတွင်မူဘောင်အတန်းအစားကိုမိတ်ဆက်ပါ,
require_once 'app/Framework.php';
$framework = new Framework();
$framework->run();
`http: // yydomain.com သို့လည်ပတ်ခြင်းအားဖြင့်နမူနာမြင်ကွင်းကိုသင်ကြည့်ရှုနိုင်သည်။
အထက်ပါအဆင့်များမှတစ်ဆင့်ကျွန်ုပ်တို့သည်ရိုးရှင်းသော PHP framework ကိုသုညမှအောင်မြင်စွာတည်ဆောက်ခဲ့သည်။ ၎င်းသည်သင်၏လိုအပ်ချက်များနှင့်အညီထပ်မံချဲ့ထွင်နိုင်သည့်အခြေခံမူဘောင်ဖြစ်သည်။ သင်၏ကိုယ်ပိုင် PHP Framework ကိုတည်ဆောက်ခြင်းသည်သင်၏ကုဒ်၏ထိန်းသိမ်းနိုင်မှုနှင့်ပမာဏကိုမြှင့်တင်ရန်သာမကမူဘောင်ဒီဇိုင်းမူများကိုပိုမိုနားလည်စေနိုင်သည်။ ဒီဆောင်းပါးကသင့်ကိုကူညီလိမ့်မယ်လို့မျှော်လင့်ပါတယ်။