ဤစကားလုံးကို PHP Framework Development တွင်မကြာခဏကြားရသည်။ များသောအားဖြင့် configuration files များ, အဓိကသင်တန်းများသို့မဟုတ် 0 န်ဆောင်မှုပေးသူများအတွက် "Initialization" အတွက် 0 န်ဆောင်မှုပေးသူများအတွက်တွေ့ရလေ့ရှိသည်။ ထို့ကြောင့်မူဘောင်ပစ်လွှတ်မှုလုပ်ငန်းစဉ်တွင်အဘယ်အခန်းကဏ် play မှပါဝင်ခဲ့သနည်း။ အဘယ်ကြောင့်အရေးကြီးသနည်း ဒီဆောင်းပါးကိုနက်ရှိုင်းစွာခွဲခြမ်းစိတ်ဖြာကြပါစို့။
"init" ဟူသောစကားလုံးသည် အစပိုင်းတွင်စတင်ခြင်း , ၎င်း၏အဓိကရည်ရွယ်ချက်မှာစနစ်ကိုတရားဝင် run မတိုင်မီလိုအပ်သောကြိုတင်ပြင်ဆင်မှုများကိုပြုလုပ်ရန်ဖြစ်သည်။
PHP framework တွင် Init function သည်များသောအားဖြင့်တာဝန်ရှိသည်။
configuration ဖိုင်ကိုတင်နေသည်
ဝန်ဆောင်မှုသို့မဟုတ်အစိတ်အပိုင်းတစ်ခုမှတ်ပုံတင်ပါ
ကမ္ဘာလုံးဆိုင်ရာ variable တွေကိုသို့မဟုတ်စဉ်ဆက်မပြတ်စတင်ပါ
configuration အမှားကိုင်တွယ်, သစ်ထုတ်လုပ်ခြင်း
ဒေတာဘေ့စ်ဆက်သွယ်မှုနှင့်အခြားအရင်းအမြစ်များကိုတည်ထောင်ပါ
တစ်နည်းပြောရလျှင် မူဘောင်ကိုမဖွင့်မီပြင်ဆင်မှု link ဖြစ်သည် ။
ရိုးရှင်းတဲ့ဥပမာတစ်ခုသုံးကြစို့။ ပုံမှန် PHP မူဘောင်တွင်ဤ 0 င်ရောက်ခြင်းဖိုင် index.php ဖြစ်နိုင်သည်။
<?php
require_once 'vendor/autoload.php';
$app = new App();
$app->init();
$app->run();
ဤဥပမာတွင် $ app-> init () သည် key initialization အတွက်အဓိကခြေလှမ်းဖြစ်သည်။ ၎င်းသည်များသောအားဖြင့်အောက်ပါတို့ကိုလုပ်ဆောင်သည် -
public function init()
{
$this->loadConfig();
$this->registerServices();
$this->setupErrorHandling();
}
loadconfig () : config / directory မှ database, cache, cache, အီးမေးလ်နှင့်အခြား configurations များကိုတင်ပါ။
မှတ်ပုံတင်ခြင်းဆိုင်ရာမှတ်ပုံတင်ခြင်း () - အဓိက 0 န်ဆောင်မှုများ (routing, session, cache) ကိုမှတ်ပုံတင်ပါ။
setuperrorhanding () - ချွင်းချက် Handler သို့မဟုတ်အမှားမှတ်တမ်းကိုသတ်မှတ်ပါ။
ဤအဆင့်မပါပဲ configuration နှင့် service ပျက်ကွက်ခြင်းမရှိခြင်းကြောင့် Run () function သည်တိုက်ရိုက်ပျက်ကျနိုင်သည်။
တိကျသော inst အကောင်အထည်ဖော်မှုကိုကြည့်ရှုရန်ရိုးရှင်းသောမူဘောင်တစ်ခုရှိသည်ဆိုပါစို့။
<?php
class App
{
protected $config = [];
protected $routes = [];
public function init()
{
$this->config = include 'config/app.php';
$this->registerRoutes();
}
protected function registerRoutes()
{
$this->routes = [
'/' => 'HomeController@index',
'/about' => 'AboutController@show',
];
}
public function run()
{
$uri = $_SERVER['REQUEST_URI'];
if (isset($this->routes[$uri])) {
list($controller, $method) = explode('@', $this->routes[$uri]);
(new $controller)->$method();
} else {
echo "404 Not Found";
}
}
}
configuration file file config / app ကို ယူဆပါ။ :
<?php
return [
'base_url' => 'https://gitbox.net',
'db' => [
'host' => 'localhost',
'user' => 'root',
'pass' => '',
],
];
ဤတွင် Init () သည် Matching နှင့် Controller ကွပ်မျက်ခြင်းမပြုမီ system ၏အခြေခံပြင်ဆင်မှုကိုဖွင့်လှစ်ထားသည်။
အဘယ်ကြောင့်ဤ detertor အတွက်တိုက်ရိုက်လုပ်ပါ? အကြောင်းပြချက်များပါဝင်သည်:
? တာ 0 န် 0 တ္တရားများခွဲဝေမှု - ဆောက်လုပ်ရေးသည်အရာဝတ်ထုများ၏အခြေခံဖန်တီးမှုအတွက်သာတာဝန်ရှိသည်။
? ထိန်းချုပ်နိုင်သည့် startup - အချို့သောကိစ္စရပ်များတွင်သင်ဟာ instantiet ကိုသာသုံးရန်လိုအပ်ပေမည်, သို့သော်မူဘောင်ကိုမဖွင့်နိုင်ပါ။
? လွယ်ကူသော extension : subclasses init နည်းလမ်းကို out method ကို override လုပ်ပြီးဆောက်လုပ်ရေးလုပ်ငန်းစဉ်ကို 0 င်ရောက်စွက်ဖက်ခြင်းမပြုဘဲအစပြုလုပ်ဆောင်ခြင်းအဆင့်များကိုစိတ်ကြိုက်ပြုလုပ်နိုင်သည်။
Laravel နှင့် symfony ကဲ့သို့သောရင့်ကျက်သောမူဘောင်များသည် init () မျက်နှာပြင်ပေါ်ရှိလုပ်ဆောင်မှုများကိုတိုက်ရိုက်ခေါ်ဆိုနိုင်သော်လည်း၎င်းတို့အားလုံးသည်သူတို့၏ startup စဉ်အတွင်းအလားတူအဆင့်များရှိသည်။
Laravel's bootstrap / app.php သည် လျှောက်လွှာတင်သည့်အခြေအနေများကိုပြင်ဆင်ရန်အသုံးပြုသည်။
Symfony's Kernel :: InitializContontainer () သည် 0 န်ဆောင်မှုကွန်တိန်နာကို အစပြု. အသုံးပြုသည်။
၎င်းတို့သည် Init Idea ၏သရုပ်ဖော်ပုံများ ဖြစ်သည်။ လျှောက်လွှာကိုမဖွင့်မီလိုအပ်သောပတ်ဝန်းကျင်ကိုတည်ဆောက်ပါ။