Introduction To Code Igniter


Published on

Lecture on CodeIgniter, # OSS Essential Training Program at Chittagong University of Engineering Technology, February 2008,

Published in: Technology

Introduction To Code Igniter

  1. 1. Introduction To CodeIgniter Mohammad Amzad Hossain Softwork Solutions [email_address]
  2. 2. Prerequisite <ul><li>OOP – Object Oriented Programming </li></ul><ul><li>PHP </li></ul><ul><li>MySQL </li></ul>
  3. 3. Index <ul><li>Introduction </li></ul><ul><ul><li>Evolution Of Web Development </li></ul></ul><ul><ul><li>Basic Idea Of Web Framework </li></ul></ul><ul><ul><li>Why Framework not Scratch? </li></ul></ul><ul><ul><li>MVC ( Model View Controller) Architecture </li></ul></ul><ul><ul><li>What is CodeIgniter ???? </li></ul></ul><ul><li>Installation of CodeIgniter </li></ul><ul><li>Application Flow of CodeIgniter </li></ul><ul><ul><li>CodeIgniter URL </li></ul></ul><ul><ul><li>Controllers </li></ul></ul><ul><ul><li>Views </li></ul></ul><ul><ul><li>Models </li></ul></ul><ul><ul><li>CodeIgniter Libraries </li></ul></ul><ul><ul><li>Helpers </li></ul></ul><ul><li>A Basic Application Development Of Codeigniter </li></ul><ul><ul><li>Application Flow </li></ul></ul><ul><li>Q/A </li></ul><ul><li>Reference </li></ul>
  4. 4. Evolution of Web Development How you first started building websites.
  5. 5. Evolution of Web Development How you’re building websites now.
  6. 6. Evolution of Web Development How you build websites with a framework
  7. 7. Basic Idea Of Web Framework <ul><li>A web application framework </li></ul><ul><ul><li>Is a Software framework </li></ul></ul><ul><ul><li>Designed to support the development of </li></ul></ul><ul><ul><ul><li>Dynamic websites </li></ul></ul></ul><ul><ul><ul><li>Web applications </li></ul></ul></ul><ul><ul><ul><li>Web services </li></ul></ul></ul><ul><ul><li>Aims to alleviate the overhead associated with common activities used in Web development. </li></ul></ul><ul><ul><ul><li>Libraries for database access </li></ul></ul></ul><ul><ul><ul><li>Templating frameworks </li></ul></ul></ul><ul><ul><ul><li>Session management </li></ul></ul></ul><ul><ul><ul><li>Often promote code reuse </li></ul></ul></ul><ul><ul><ul><li>Many more ……. </li></ul></ul></ul>
  8. 8. Why Framework Not Scratch ? <ul><li>Key Factors of a Development </li></ul><ul><ul><li>Interface Design </li></ul></ul><ul><ul><li>Business Logic </li></ul></ul><ul><ul><li>Database Manipulation </li></ul></ul><ul><ul><li>User Access Control </li></ul></ul><ul><li>Advantage of Framework </li></ul><ul><ul><li>Templating </li></ul></ul><ul><ul><li>Provide Solutions to Common problems </li></ul></ul><ul><ul><li>Abstract Levels of functionality </li></ul></ul><ul><ul><li>Make Rapid Development Easier </li></ul></ul><ul><li>Disadvantage of Scratch Development </li></ul><ul><ul><li>Make your own Abstract Layer </li></ul></ul><ul><ul><li>Solve Common Problems Yourself </li></ul></ul><ul><ul><li>The more Typing Speed the more faster </li></ul></ul>
  9. 9. MVC Architecture <ul><li>Separates User Interface From Business Logic </li></ul><ul><li>Model - Encapsulates core application data and functionality Business Logic. </li></ul><ul><li>View - obtains data from the model and presents it to the user. </li></ul><ul><li>Controller - receives and translates input to requests on the model or the view </li></ul>Figure : 01
  10. 10. What is CodeIgniter ??? <ul><li>An Open Source Web Application Framework </li></ul><ul><li>Nearly Zero Configuration </li></ul><ul><li>MVC ( Model View Controller ) Architecture </li></ul><ul><li>Multiple DB (Database) support </li></ul><ul><li>DB Objects </li></ul><ul><li>Templating </li></ul><ul><li>Caching </li></ul><ul><li>Modules </li></ul><ul><li>Validation </li></ul><ul><li>Rich Sets of Libraries for Commonly Needed Tasks </li></ul><ul><li>Has a Clear, Thorough documentation </li></ul>
  11. 11. Installation of CodeIgniter <ul><li>Requirments </li></ul><ul><ul><li>Web Server - Download & Install Apache </li></ul></ul><ul><ul><li>PHP – 4.3.2 or Higher </li></ul></ul><ul><ul><li>Database – MySQL ( support for other DB exists ) </li></ul></ul><ul><li>Installation </li></ul><ul><ul><li>Download the latest version from and unzip into your web root directory. </li></ul></ul><ul><ul><li>Open “application/config/config.php” and change base_url value to base url. For example: http://localhost/myci/ </li></ul></ul><ul><ul><li>To Use Database open “application/config/database.php” and change necessary values. Usually you have to change: ‘hostname’, ‘username’, ‘password’, ‘datbase’. </li></ul></ul><ul><ul><li>Start Your Web Server and Database Server and go to http:// localhot/myci </li></ul></ul>
  12. 12. Application Flow Of CodeIgniter Figure : 2 [ Application Flow of CodeIgniter]
  13. 13. CodeIgniter URL <ul><li>URL in CodeIgniter is Segment Based. </li></ul>Segments in a URI CodeIgniter Optionally Supports Query String URL news / article / my_article class / function / ID c=news & m=article & ID=345
  14. 14. Controllers <ul><li>A Class file resides under “application/controllers” </li></ul> first <?php class First extends Controller{ function First() { parent::Controller(); } function index () { echo “<h1> Hello CUET !! </h1> “; } } ?> // Output Will be “Hello CUET!!” <ul><li>Note: </li></ul><ul><ul><li>Class names must start with an Uppercase Letter. </li></ul></ul><ul><ul><li>In case of “constructor” you must use “parent::Controller();” </li></ul></ul>
  15. 15. Controllers <ul><li>In This Particular Code </li></ul><?php class First extends Controller{ function index() { echo “<h1> Hello CUET !! </h1> “; } function bdosdn ( $location ) { echo “<h2> Hello $location !! </h2>”; } } ?> // Output Will be “Hello world !!” first / bdosdn/ world <ul><li>Note: </li></ul><ul><ul><li>The ‘Index’ Function always loads by default. Unless there is a second segment in the URL </li></ul></ul>
  16. 16. VIEWS <ul><li>A Webpage or A page Fragment </li></ul><ul><li>Should be placed under “application/views” </li></ul><ul><li>Never Called Directly </li></ul><html> <title> My First CodeIgniter Project</title> <body> <h1> Welcome ALL … To My .. ::: First Project ::: . . . </h1> </body> </html> web_root/myci/system/application/views/myview.php
  17. 17. VIEWS <ul><li>Calling a VIEW from Controller </li></ul>Data Passing to a VIEW from Controller $this->load->view(‘myview’); function index() { $var = array( ‘ full_name’ => ‘Amzad Hossain’, ‘ email’ => ‘’ ); $this->load->view(‘myview’, $var); } <html> <title> ..::Personal Info::.. </title> <body> Full Name : <?php echo $full_name;?> <br /> E-mail : <?=email;?> <br /> </body> </html>
  18. 18. VIEWS <ul><li>There are 3 mechanism that can be utilize to show Dynamic Data inside a VIEW File </li></ul><ul><li>- Pure PHP </li></ul><ul><li>- PHP’s Alternative Syntax </li></ul><ul><li>- CodeIgniter’s Template Engine </li></ul><!-- PHP’s Alternative Syntax --> <?php if( $for_this == true ):?> <h1> For This </h1> <?php elseif( $for_that == true ): ?> <h1> For That </h1> <?php else: ?> <h1> What </h1> <?php endif; ?> <ul><li>Note: </li></ul><ul><ul><li>There are other alternative syntax ‘for’, ‘foreach’, ‘while’ </li></ul></ul>
  19. 19. Models <ul><li>Designed to work with Information of Database </li></ul><ul><li>Models Should be placed Under “application/models/” </li></ul>Loading a Model inside a Controller <?php class Mymodel extend Model{ function Mymodel() { parent::Model(); } function get_info () { $query = $this->db->get(‘name’, 10); /*Using ActiveRecord*/ return $query->result(); } } ?> $this->load->model(‘mymodel’); $data = $this->mymodel->get_info();
  20. 20. CodeIgniter Libraries Special Purpose Classes Loading CodeIgniter Library Benchmarking Database Encryption Calendaring FTP Table File Uploading Email Image Manipulation Pagination Input and Security HTML Trackback Parser Session Template Unit Testing User Agent URI Validation $this->load->library(‘database’);
  21. 21. CodeIgniter Libraries <ul><li>Database Library </li></ul><ul><li>Abstract Database Class support traditional structures and Active Record Pattern. </li></ul>Active Record Pattern General Approach function index() { $this->load->library(‘database’); $rslt = $this->db->query (“ select first_name from user_name ”); foreach( $rslt->result() as $row_data) echo $row_data->first_name . “<br />”; } function index() { $this->load->library(‘database’); $this->db->select(“first_name”); $rslt = $this->db->get (“ user_name ”); foreach( $rslt->result() as $row_data) echo $row_data->first_name . “<br />”; }
  22. 22. Helpers <ul><li>Simply a collection of functions in a particular category. </li></ul>Loading A Helper Inside a Controller Array Date File HTML Smiley Text URL Cookie Download Form Security String Directory E-mail Inflector XML Parser Typography $this->load->helper(‘helper_name’); $this->load->helper(array(‘form’,’url’) );
  23. 23. Helpers <ul><li>Form Helper </li></ul><ul><ul><li>form_open() </li></ul></ul><ul><ul><li>form_open_multipart() </li></ul></ul><ul><ul><li>form_input() </li></ul></ul><ul><ul><li>form_textarea() </li></ul></ul><ul><ul><li>form_checkbox() </li></ul></ul><ul><ul><li>form_submit() </li></ul></ul><ul><ul><li>form_close() </li></ul></ul><ul><li>URL Helper </li></ul><ul><ul><li>site_url() </li></ul></ul><ul><ul><li>base_url() </li></ul></ul><ul><ul><li>anchor() </li></ul></ul><ul><ul><li>anchor_popup() </li></ul></ul><ul><ul><li>mailto() </li></ul></ul>
  24. 24. A Personal Blog Using CI
  25. 25. Questions & Answers
  26. 26. Useful Links <ul><li> </li></ul><ul><li> </li></ul>
  27. 27. Reference <ul><li>User Guide of CodeIgniter 1.6.1 </li></ul><ul><li>Wikipedia </li></ul><ul><li>PHPit </li></ul><ul><li>Slideshare </li></ul>