CodeIgniter

CodeIgniter කියන්නේ වර්තමානයේ බහුලව  බාවිතා  වන open source web application framework එකක් ..CodeIgniter site එකෙන් නොමිලේම download කරලා install කරගන්න පුළුවන්.codeIgniter වල  බාවිතා වෙන්නේ MVC design pattern එක..

Model-View-Controller(MVC) Development Pattern

MVC කියන්නේ software design pattern එකක්.සාමාන්‍ය web architecture එකේදී  වෙන්නේ web browser එක web page එක සමග  ගනුදෙනු කරලා output එකක් ලබා දෙන එක..සරල ලෙස මේ විදිහට පෙන්නන පුළුවන්..මේ file එක ඇතුලේ සියලුම logical operations,database interactions ඇතුළුව සියලුම දේ තියෙන්න පුළුවන්.asasa

MVC architecture එකේදී  වෙන්නේ  මේ  සියලුම දේවල්  එක page එකක බාවිතා නොකර එය models,views,controllers ලෙස කොටස්  වලට  වෙන් කරලා බාවිතා කරන එක..

controllers- controllers වලින් සියලුම decisions පාලනය වෙනවා.

models-models මගින් සිදුවන්නේ data handling

views- views වලින් වෙන්නේ සියලුම user interfaces(presentations) handle කරන එක.2000px-MVC-Process.svg

CodeIgniter install කරගත්තම මේ විදිහට default page එකක් බලාගන්න පුළුවන්.

ciCodeIgniter URL processing

CodeIgniter වල URL එක  call කරන්න ඕන මේ  පිළිවෙලට

basic url:/ {controller}/{method}/{arguments}

උදාහරණයක් ලෙස default page එකට  අදාළ url එක සලකමු..ci2

මෙහිදී basic url- http://localhost/CodeIgniter/index.php

welcome controller එක තුල  තියෙන index method එක  call  කරාම  මේ විදිහට view එකක් බලාගන්න පුළුවන්.ci2

application =>controllers=>Welcome.php තුල තමා welcome controller එක  තියෙන්නේ..

<?php

 defined('BASEPATH') OR exit('No direct script access allowed');

 class Welcome extends CI_Controller{
 
      public function index(){

           $this->load->view('welcome_message');
	}
}

මේ index method එකෙන් application=>views=>welcome_message.php තුල  තියෙන html code එකට  අදාළ view එක ලබාදෙනවා.

(extends වලින්  වෙන  දේ  ගැන මෙතනින් බලන්න පුළුවන් )

අලුත් Controller එකක් හදන්නේ කොහොමද?

application=>controllers folder එක  ඇතුලේ  තමයි සියලුම controllers තියෙන්න ඕන.

aaමෙහි Cars.php file එක ඇතුලේ Cars controller එක හදලා තියෙනවා..Cars controller එක ඇතුලේ  audi() method එක තියෙන්නේ..මේ method එක call කරාම views folder එකේ  තියෙන audi.php එකට  අදාළ html view එක  ලබාදෙන්න ඕන..

$this->load->view(‘audi’); මේකෙන් සිද්ද වෙන්නේ ඒ දේ.

sseදැන් controller එක සහ එයට අදාලව view එකක් හදාගෙන ඉවරයි.දැන් මේ view එක browser එකේ  පෙන්නන ඕන..කලින් කිව්ව විදිහට base url එකේ ඉදන් {controller}/{method} මේ පිළිවෙලට තමා url එක  process වෙන්න ඕන.

dsmodels

කොහොමද models බාවිතා  කරන්නේ..model එක  ඇතුලේ සාමාන්යෙන්   තියෙන්නේ

1.Application data

2.Logic and Business Rules

3.Functions

අපි  දැන් හදන model එකට database connection එකක්  අවශ්‍ය  වෙනවා.එකට database configurations හදාගන්න ඕන.

application=>config=>database.php

4ඒ වගේම autoload libraries වලට database library එක add කරන්න ඕන.මෙහිදී සිද්ද වෙන්නේ background එකෙන් database එක autoload වෙන එක.

application=>config=>autoload.php

4දැන් අපිට database එකේ තියෙන data ලබාගන්න model එකක් හදන්න පුළුවන්..

application=>models folder එක  ඇතුලේ Data.php file එකේ තමයි Data model එක තියෙන්නේ.getusers() method එකෙන් තමා database එකෙන් data ලබාගන්නේ.

2

 

 


public function getusers(){

        $query= $this->db->query("SELECT * FROM users");

        return $query->result();

    }


මේ method එකෙන් users table එකේ තියෙන සියලුම data array එකක් විදිහට controller එකකට pass කරන්න පුළුවන්..කලින් හදාගත්ත Car controller එකේම Data model එක call කරන්න පුළුවන්.


public function getusers(){

        $query= $this->db->get("users");

        return $query->result();

    }


මේ විදිහට get method එකෙන් සියලුම data fetch කරන්නත් පුළුවන්… දැන් Car controller එකේ Data model එක  call කරමු..

2getdata() method එකෙන් තමා model එක call කරන්නේ..

$this->load->model(‘Data’); —–> Data model එක  controller එකට  load වෙනවා

$data[“results”]= $this->Data->getusers(); —-> Data model එකේ getusers() method එක call කරනවා..මෙහිදී return වෙන array එක $data variable එකට  assign කරගන්නවා

$this->load->view(‘viewdb’,$data); —–> views folder එක තුල තියෙන  viewdb.php  file එක load කරනවා..ඒ අතරම viewdb.php file එකට $data array එක pass කරනවා.

viewdb.php file එකේ table එකක මේ data පෙන්නන පුළුවන්..

asss
4codeigniter configurations

codeigniter වල  සෑම request එකක්ම index.php file එක  හරහා run වෙන්නේ..ඒ නිසා url එකේ index.php අනිවාර්යෙන්ම තියෙන්න ඕන..නමුත් url rewrite කරලා අපිට  මේ index.php අයින් කරන්න පුළුවන්.. root directory එකට .htaccess file එකක් add කරලා url rewrite කරන්න පුළුවන්.මේ සදහා apache server එකේ rewrite_mod active කරලා තියෙන්න ඕන .

4

 

 


RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]


දැන් index.php නැතුව run කරන්න පුළුවන්..

2routing

controller එක සහ එයට අදාළ  method එක බාවිතා කරනවට වඩා පහසු අර්ථ  සහිත url එකක් හදාගන්න පුළුවන් routing system එකෙන්..

Cars/getdata කොටස carusers ලෙස rewrite කරන්නේ මෙහෙමයි..

application=>config=>routes.php

2$route[‘carusers’]= ‘Cars/getdata’;

4දැනට ඉවරයි …comment එකක්  දාලම  යන්න!!!!!!!!!!!!!!!!!!!!!!!!

35 thoughts on “CodeIgniter

Leave a comment