Extend in CodeIgniter Framework
Saturday, 28 August 2010 22:23
ဒီ Post မွာ အဓိကထားျပီး ေရးမွာ ကေတာ့ CodeIgniter Framework ေပၚမွာ ရွိေနျပီးသား Library, Helper ေတြနဲ႔ မလုံေလာက္ ရင္ ျဖစ္ေစ၊ ကိုယ့္ Project Requirement အရ ေရးထည့္ဖို႔ လိုလာရင္ ျဖစ္ေစ Library, Helper အသစ္ေတြကို ဘယ္လိုထည့္ မလဲ ဆိုတာကို ျဖစ္ပါတယ္။ တစ္ နည္း အားျဖင့္ CI Framework ကိုဘယ္လို Extend လုပ္မလဲ ဆိုတာလဲ ျဖစ္ပါတယ္။ ကြၽန္ေတာ္ ေရးထားျပီးသား CI Post ေတြကို ျပန္ဖတ္ခ်င္ရင္ေတာ့
-
CodeIgniter and MVC for Beginners
-
Structure and Configuration of CodeIgniter
-
CodeIgniter Database Library
-
CodeIgniter Form Helper
-
Simple Data Entry in CodeIgniter
-
Pagination with CodeIgniter
-
File upload in CodeIgniter
How to add own Helper
Okay အရင္ဆုံး helper တစ္ခုဘယ္လို ေပါင္းထည့္ မလဲ ဆိုတာေလး ၾကည့္ရေအာင္။ CI Structure မွာ application နဲ႔ system ရွိေနျပီး Built-In Library ေတြ၊ Helper ေတြအားလုံးက system ေအာက္မွာပဲ ရွိတယ္ဗ်ာ။ ကိုယ့္ အသစ္ထည့္ ခ်င္တာကို အဲဒီ Folder ေတြေအာက္မွာ သြားမထည့္ ရပါဘူး။ ဒါဆိုဘယ္မွာ ထည့္ရမလဲ ဆိုေတာ့ ... application folder ေအာက္မွာလည္း helpers, libraries folder ေတြရွိေနပါတယ္။ အဲဒီမွာ ထည့္ေပးရပါမယ္။

ကြၽန္ေတာ္ကေတာ့ helpers folder ေအာက္မွာ calculation_helper.php ဆိုျပီး php file အသစ္တစ္ခု ေဆာက္လိုက္တယ္။ အဲဒီမွာ မွတ္သား စရာ တစ္ခုကေတာ့ file name မွာ ေနာက္က _helper ဆိုတာပါရပါမယ္။ ဆိုလိုတာကဗ်ာ abc_hepler.php, xyz_helper.php စသည္ျဖင့္ ေရွ႕က ျဖစ္ခ်င္တာ ျဖစ္ျပီး ေနာက္က _helper.php နဲ႔ ဆုံးေပးရမယ္။ ျပီးေတာ့ ေရးေနၾကအတိုင္းပဲ function ေရးလို႕ ရပါတယ္။ ကြၽန္ေတာ္ကေတာ့ function add_value ကိုေရးထားလိုက္ပါတယ္။
<?php
function add_value($val1,$val2){
return $val1 + $val2;
}
အဲဒီ Code ကို Controller ထဲက ေခၚ ၾကည့္ဖို႔ controller တစ္ခုကို ဆက္ျပီးေဆာက္မယ္။
<?php
class Test extends Controller{
function calculate(){
$this->load->helper('calculation');
$val1 = 5;
$val2 = 3;
echo "Result is " . add_value($val1,$val2);
}
}
test.php ဆိုျပီး နာမည္ေပး လိုက္မယ္ဗ်ာ။ ဒါက ခုန ကြၽန္ေတာ္ေရးထားတဲ့ Helper calculation ကို loading လုပ္တာပါ။ $this->load->helper('calculation');
ကြၽန္ေတာ္ လုပ္ထားတာ calculation_helper ျဖစ္ေပမယ့္ ျပန္ေခၚေတာ့ _helper မပါေတာ့ပါဘူး။ တကယ္လို႕ ကိုယ့္က abc_helper.php ကိုလုပ္ထားခဲ့ မယ္ဆိုရင္ေတာ့ $this->load->helper('abc'); ေပါ့ဗ်ာ။ Browser ကေန ဒီလို Run လိုက္မယ္ ဆိုရင္ေတာ့
http://localhost:8888/ci_extend/index.php/test/calculate/
ႏွစ္ခု ေပါင္းထားတဲ့ result ကိုျမင္ရဖို႔ သုံးသြားတဲ့ add_value ဆိုတာကေတာ့ calculation_helper.php ထဲက ေခၚသြားတာ ျဖစ္ပါတယ္။
ဒါဆိုရင္ေတာ့ CI မွာ Helper အသစ္တစ္ခု ဘယ္လိုထည့္ မယ္ဆိုတာ Idea အၾကမ္းေတာ့ ရမယ္ ထင္ပါတယ္။ 
How to add own Library
Library အသစ္တစ္ခု ဘယ္လိုေပါင္းထည့္ မလဲ ဆိုတာကို ဆက္ျပီး ၾကည့္လိုက္ရေအာင္။ application ေအာက္က libraries folder မွာ ကိုယ္ထည့္ ခ်င္တဲ့ library အသစ္တစ္ခုကို ထည့္ေပးရမယ္။ ကြၽန္ေတာ္က hello ဆိုတဲ့ Library ကိုထည့္ ဖို႔ ဒီလိုေလး ေရးလိုက္တယ္ဗ်ာ။ file name ကေတာ့ hello.php ေပါ့ဗ်ာ။ <?php
class hello{
function message(){
echo "How are you?";
}
}
?>
အဲဒီမွာ class hello ဆိုရင္ ရပါျပီ။ controllers ေတြ model ေတြေရးတုန္းကလို extends လုပ္စရာမလိုပါ။ ကြၽန္ေတာ္ေရးထားတဲ့ message ကို controller ကေန သုံးၾကည့္ဖို႔ ခုနကြၽန္ေတာ္ ေရးထားျပီးသား controller file ျဖစ္တဲ့ test.php မွာ ဒီ code ေလးကို ထပ္ေပါင္းလိုက္ရေအာင္။
function hello_lib(){
$this->load->library('hello');
$this->hello->message();
}
ျပီးရင္ေတာ့ Browser ကေန ဒီလိုေလး run ၾကည့္မယ္ ဆိုရင္ေတာ့ http://localhost:8888/ci_extend/index.php/test/hello_lib/
"How are you?" ဆိုတဲ့ message ကိုျမင္ရမွာ ျဖစ္ပါတယ္။
How to use resource from existing Libraries
အဲဒီ ကိုယ္ထည့္လိုက္တဲ့ Library မွာ ရွိေနျပီးသား library ေတြထဲက value ေတြ function ေတြကို ျပန္သုံးခ်င္ရင္ေရာ ဘယ္လို လုပ္မလဲ ဆိုတာေလးကို ၾကည့္ရေအာင္။ ခုန ေရးထားတဲ့ Library ထဲမွာ ဒီ code ေလးကိုထည့္ ၾကည့္မယ္ေနာ္။
function message(){
echo "How are you?";
$this->load->library->('config');
}
အရင္ အရင္ ေတြတုန္းက controllers, models ေတြထဲကေန အဲလို သုံးရင္ error မတတ္ေပမယ့္ ခုခ်ိန္မွာေတာ့ ဒီလို error တတ္မယ္ဗ်ာ။

ဘာလိုလဲ ဆိုေတာ့ $this က point လုပ္ေနတာက hello class ကိုျဖစ္ေနလိုပဲ ျဖစ္ပါတယ္။ ဒီေတာ့ super class object ကိုျပန္ယူေပးဖို႔ လိုပါတယ္။ အဲဒါေၾကာင့္ Library ကို ဒီလိုေလး ျပင္လိုက္မယ္။
<?php
class hello{
var $CI;
function hello(){
$this->CI = & get_instance();
}
function message(){
echo "How are you?";
$this->CI->load->library('config');
echo "My Languages is " . $this->CI->config->item('language');
//$this->load->library->('config');
}
}
?>
Class ထဲမွာ သုံးဖို႔ CI ဆိုတဲ့ variable ထဲကို $this->CI = & get_instance(); ဆိုျပီး super object ကို Reference(&) ယူလိုက္ပါတယ္။ အဲဒါေၾကာင့္ ဒီေနရာေတြမွာ $this->CI->load->library('config'); လို႕ျပန္သုံးတဲ့ အခါ error မတတ္ေတာ့ပဲ သုံးလို႕ ရသြား တာ ျဖစ္ပါတယ္။ ေနာက္ Post မွာ CKEditor Library ကိုဘယ္လို ေပါင္းထည့္ မလဲ ဆိုတာ ေရးပါမယ္။ ဖတ္ျပီး နားမလည္တာ မွားေနတာ မရွင္းတာေတြ ရွိေနရင္လည္း comment ေပးေဆြးေႏြး ေပးဖို႔ ေတာင္းဆိုပါတယ္။ 
Ref: http://net.tutsplus.com/tutorials/php/codeigniter-from-scratch-extending-the-framework/
| < Prev | Next > |
|---|
Login Form
Latest Post
Categories Table View
- Reader's Conner (133)
- PHP (48)
- Joomla CMS (46)
- Codeigniter (18)
- jQuery (12)
- iDhamma (11)
- Mobile Development (10)
- PHP & AJAX (4)
- Apache (3)
- For Mac (3)
- mySQL (2)
- DhammaDroid (1)
Sorry for posting here as I couldn't find contact page.
I was reading your posts and it is really good for me to learn php since I am a joomla user. It is really good to read in Burmese. While I was exploring your blog, I found that this url is missing.
It redirected to homepage.
http://www.zack-notes.net/joomzack15/php/php-syntax.html
thanks in advance.
it is fine with me to delete this comment after resolving the link.
Aung Aung