Basic of Codeigniter
Short Description
This is a codeigniter basic tutorial. This document clearly explain about MVC design pattern, routing of codeigniter, te...
Description
Speaker:
JhA KUMAR PANKA J
Our Agenda ➢
What is CodeIgniter ?
➢
What makes CodeIgniter a smart framework to use?
➢
Why MVC?
➢
Routing Basics
➢
Installation and Configuration
➢
Model View Controller
➢
Template Integration
➢
Template Engine
What is CodeIgniter ? ➢
A proven, agile & open PHP web application fr amework
➢
Enables developers to build web applications faster
➢
Offers many helpful code libraries and helpers
➢
Based on MVC design pattern
➢
Developed by EllisLab
What makes CodeIgniter a smart framework? ➢
Exceptional performance
➢
MVC approach to development
➢
Generates search engine friendly clean URLs
➢
Easily extensible
➢
Runs on both PHP 4 (4.3.2+) and 5
➢
Support for most major databases including MySQL (4.1+), MySQLi, MS SQL, Postgres, Oracle, SQLite, and ODBC.
➢
Application security is a focus
➢
Easy caching operations
➢
Many libraries and helpers to help you with complex operations such as email, image manipulation,form validation etc
➢
Most libraries are only loaded when needed which cuts back on resources needed
Why MVC? ➢
MVC stands for Model, View, Controller
➢
Model: Model: The model deals with the raw data and database interaction. Component is not required and can be included in the controller.
➢
View: View: The view deals with displaying the data and interface controls to the user.
➢
Controller: The controller acts as the in between of view and model.The controller is the place to load libraries and helpers.
Routing Basics
[ GlobAl CodESTER
Installation and Configuration
]
Download CodeIgniter ➢
Download CodeIgniter and upload it to your server. http://ellislab.com/codeigniter/download
➢
All you need to do is unzip and up load it to your PHP and MySQL enabled server.
CI Directory Structure ➢
The application directory contains model,view,controller,helper,library model,view,controller,helper,library and so on.
➢
The system directory is the core and consists of the core framework files. When a new version is released, you can update your existing application just by replacing this system directory with the latest release.
➢
The user_guide houses user_guide houses the user guide to CI.
➢
The index.php file index.php file is the bit that does all the CI magic.
Application Directory Structure ➢
cache folder stores all the caches generated by the caching library. The cache folder
➢
This config directory config directory includes settings/configuration related information like database settings, route information etc.
➢
The core directory needed to extend the functionality of core classes like controller,loader,router etc.
➢
The errors folder stores all the template error pages for the application.
➢
The helpers folder stores all the helpers which are specific to your application like email, imageMagick etc.
➢
The hooks folder is for hooks which modify the functioning of CI’s core files.
➢
The language folder stores lines of text which can be loaded through the language library to create multilingual sites.
➢
The libraries folder stores all the libraries which are specific to your application.
➢
The third_party directory third_party directory will include library files, but only those, which are imported from third-party.
System Directory Structure ➢
The database folder stores all the database drivers and class which enable you to connect to database.
➢
The fonts folder fonts folder stores all the fonts which can be used by the image manipulation library.
➢
The helpers folder helpers folder stores all of CI’s core helpers but you can place your own helpers in here which can be accessed by all of your applications.
➢
The language folder stores all of CI’s core language files which its libraries and helpers use.
➢
The libraries folder stores all of CI’s core libraries but you can place your own libraries.
Configuration ➢
Need to set up “base_url” up “base_url”.. To do this, open up system/application/config/config.php
➢
Need to set up “database “ database”” To use a database, open up system/application/config/database.php
➢
Add “database “database”” to the autoload libraries To do this, open up system/application/config/autoload.php
Testing CodeIgniter
y ! l l f u s s c c e u s e r t t i g n I g e o d C r e d u g i g n f o u c o n , y o o i a t l u r a t g n C o
[ GlobAl CodESTER
Model View Controller
]
What is a controller? A Controller is simply a class file that is named in a way that can be associated with a URI. http://localhost/news_system/index.php/news http://localhost/news_system/index.php/ news Here CI will find news.php Controller
Let's create a simple news controller so you can see it in action. Then save the file “news.php” to “application/controllers/” folder. class News extends CI_Controller { public function __construct() { parent::__construct(); $this->load->model('news_model'); } public function index() { $data['news'] = $this->news_model->get_news(); $data['title'] = 'News archive'; $this->load->view('templates/header', $data); $this->load->view('news/index', $data); $this->load->view('templates/footer'); } }
Important Note: Class names must start with an uppercase letter.
Passing URI segments to function function name
class News extends CI_Control CI_Controller ler {
http://localhost/news_system/index.php/news/view/initializing-the-class http://localhost/news_system/index.php/news/view/ initializing-the-class
public function function __construct() __construct() {
parent::__construct();
CI allow to pass more than t han one segments } }
Class Constructors If you intend to use a constructor in Controllers, MUST MUST place place parent::__construct(); Constructors are useful if you need to set some default values, or run a default process when your class is instantiated.
What is a Model? Models are PHP classes that are designed to work with information in your database. Basic prototype for News model class: class News_model extends CI_Model { public function __construct() { $this->load->database(); } }
Class names must have the first letter capitalized
an t No te ta mpor t Im I File name will be lower case version of class name i.e i .e news_model.php
Loading a Model $this->load->model('Model_name');
Use a Model $this->Model_name->method();
Active Record Class ➢
CodeIgniter uses a modified version of the Active Record Database Pattern.
➢
Pattern allows information to be retrieved retrieved,, inserted inserted,, and updated updated in in your database with minimal scripting.
➢
A major benefit to using the Active Record features Record features is that it allows you to create database independent applications
Build SQL Select statement ➢
$query = $this->db->get $this->db->get('mytable'); ('mytable'); // Produces: SELECT * FROM mytable
➢
The second and third parameters enable you to set a limit and offset clause: $query = $this->db->get $this->db->get('mytable', ('mytable', 10, 20); // Produces: SELECT * FROM mytable LIMIT 20, 10
➢
$query = $this->db->get_where $this->db->get_where('mytable', ('mytable', array('id' => $id), $limit, $offset); // Produces: SELECT * FROM mytable where id=$id LIMIT $offset, $limit
➢
$this->db->select('title, $this->db->select ('title, content, date'); $query = $this->db->get('m $this->db->get('mytable'); ytable'); // Produces: SELECT title, content, date FROM mytable
Build SQL Select statement ➢
$this->db->select_max('age', $this->db->select_max ('age', 'member_age'); 'member_age'); $query = $this->db->get('members'); $this->db->get('members'); // Produces: SELECT MAX(age) as member_age FROM members
➢
$this->db->order_by('title $this->db->order_by ('title desc, name asc'); // Produces: ORDER BY title DESC, name ASC
➢
$names = array('Frank', 'Todd', 'James'); $this->db->where_in('username', $this->db->where_in ('username', $names); // Produces: WHERE username IN ('Frank', 'Todd', 'James')
Build SQL Insert statement ➢
$data = array( 'title' => 'My title' , 'name' => 'My Name' , 'date' => 'My date'); $this->db->insert('mytable', $this->db->insert ('mytable', $data); // Produces: INSERT INTO mytable (title, name, date) VALUES ('My title', 'My name', 'My date')
➢
$this->db->set('name', $this->db->set ('name', $name); $this->db->insert('mytable'); // Produces: INSERT INTO mytable (name) VALUES ('{$name}') OR $array = array('name' => $name); $this->db->set($array); $this->db->insert('mytable'); //You can also pass an associative array to this function
Build SQL Update statement ➢
$data = array('title' => $title,'name' => $name,'date' => $date); $this->db->where('id', $this->db->where('id', $id); $this->db->update('mytable', $this->db->update ('mytable', $data); // Produces: // UPDATE mytable SET title = '{$title}', name = '{$name}', date = '{$date}' WHERE id = $id
➢
$data = array( array('title' => 'My 'My title','name' => => 'My Name 2' ,'date' => 'My date 2' ), array('title' => 'Another title' ,'name' => 'Another 'Another Name 2' ,'date' => 'Another date 2' ) ); $this->db->update_batch('mytable', $data, 'title'); $this->db->update_batch('mytable', // Produces: // UPDATE `mytable` SET `name` = CASE WHEN `title` = 'My titl e' THEN 'My Name 2' WHEN `title` = 'Another title' THEN 'Another Name 2' ELSE `name` END, `date` = CASE WHEN `title` = 'My title' THEN 'My date 2' WHEN `title` = 'Another title' THEN 'Another date 2' ELSE `date` END WHERE `title` IN ('My title','Another title')
Build SQL Delete statement ➢
$this->db->delete('mytable', $this->db->delete ('mytable', array('id' => $id)); // Produces: // DELETE FROM mytable WHERE id = $id
➢
$this->db->from('mytable'); $this->db->truncate(); $this->db->truncate (); // or
Note: If the TRUNCATE command isn't available, truncate() will execute as "DELETE FROM table".
$this->db->truncate('mytable'); $this->db->truncate ('mytable'); // Produce: // TRUNCATE mytable
What is View? ➢
A view is a web page, or a page fragment, like a header, footer, sidebar, etc.
➢
Views are never called directly, they must be loaded by a controller
My Blog
Loading a View $this->load->view('name');
Welcome to my Blog!
Helper ➢
Helpers means “help you with tasks”.
➢
Each helper file is simply a collection of functions or methods.
➢
Stored in your system/helpers, system/helpers, or application/helpers directory
Few common Helpers are listed below: ➢
URL Helpers, Helpers, that assist in creating links
➢
Form Helpers that Helpers that help you create form elements
➢
Text Helpers perform Helpers perform various text formatting routines
➢
Cookie Helpers set Helpers set and read cookies
➢
File Helpers help Helpers help you deal with files
Loading a Helper $this->load->helper('name');
[ GlobAl CodESTER
]
Template Integration
Where we put css/js/image css/js/image directory directory ? ➢
Local CI Directory “news_system”
➢
Configure “base_url“ “base_url“ & & define one more variable “BASE_URI” To do this, open up system/application/config/config.php
➢
Directory structure of project “news_system”
Create templates directory ➢
Create a “templates” “templates” directory directory To do this, open up application/views/
➢
Create “header.php” “header.php” & & “footer.php” files in “templates” directory
Link to css/js/images ➢
For “css” “css” directory directory
View more...
Comments