Sunteți pe pagina 1din 33

Speaker:

UMAR JhA PANKAJ K odESTER] [GlobAl C

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

GlobAl CodESTER

What is CodeIgniter ?
A proven, agile & open PHP web application framework Enables developers to build web applications faster Offers many helpful code libraries and helpers Based on MVC design pattern Developed by EllisLab

GlobAl CodESTER

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

GlobAl CodESTER

Why MVC?
MVC stands for Model, View, Controller Model: The model deals with the raw data and database interaction. Component is not required and can be included in the controller. 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.

GlobAl CodESTER

Routing Basics

GlobAl CodESTER

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 upload it to your PHP and MySQL enabled server.

GlobAl CodESTER

CI Directory Structure
The application directory contains 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 the user guide to CI. The index.php file is the bit that does all the CI magic.

GlobAl CodESTER

Application Directory Structure


The cache folder stores all the caches generated by the caching library. This 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 CIs 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 will include library files, but only those, which are imported from third-party.

GlobAl CodESTER

System Directory Structure


The database folder stores all the database drivers and class which enable you to connect to database. The fonts folder stores all the fonts which can be used by the image manipulation library. The helpers folder stores all of CIs 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 CIs core language files which its libraries and helpers use. The libraries folder stores all of CIs core libraries but you can place your own libraries.

GlobAl CodESTER

Configuration
Need to set up base_url.
To do this, open up system/application/config/config.php

Need to set up database


To use a database, open up system/application/config/database.php

Add database to the autoload libraries


To do this, open up system/application/config/autoload.php

GlobAl CodESTER

Testing CodeIgniter

Co

ng

f on c ou y , n tio a ul rat

r igu

ed

eIg d Co

c su r it e

ce

ul f s s

! ly

GlobAl CodESTER

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
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.

GlobAl CodESTER

Passing URI segments to function


function name http://localhost/news_system/index.php/news/view/initializing-the-class
class News extends CI_Controller {

public function __construct() {

CI allow to pass more than one segments


}
}

parent::__construct();

Class Constructors
If you intend to use a constructor in Controllers, MUST place parent::__construct(); Constructors are useful if you need to set some default values, or run a default process when your class is instantiated.

GlobAl CodESTER

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

Importan

t Note

File name will be lower case version of class name i.e news_model.php

Loading a Model
$this->load->model('Model_name');

Use a Model
$this->Model_name->method();

GlobAl CodESTER

Active Record Class


CodeIgniter uses a modified version of the Active Record Database Pattern. Pattern allows information to be retrieved, inserted, and updated in your database with minimal scripting. A major benefit to using the Active Record features is that it allows you to create database independent applications

GlobAl CodESTER

Build SQL Select statement


$query = $this->db->get('mytable'); // Produces: SELECT * FROM mytable The second and third parameters enable you to set a limit and offset clause: $query = $this->db->get('mytable', 10, 20); // Produces: SELECT * FROM mytable LIMIT 20, 10 $query = $this->db->get_where('mytable', array('id' => $id), $limit, $offset); // Produces: SELECT * FROM mytable where id=$id LIMIT $offset, $limit $this->db->select('title, content, date'); $query = $this->db->get('mytable'); // Produces: SELECT title, content, date FROM mytable
[

GlobAl CodESTER

Build SQL Select statement


$this->db->select_max('age', 'member_age'); $query = $this->db->get('members'); // Produces: SELECT MAX(age) as member_age FROM members $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', $names); // Produces: WHERE username IN ('Frank', 'Todd', 'James')

GlobAl CodESTER

Build SQL Insert statement


$data = array( 'title' => 'My title' , 'name' => 'My Name' , 'date' => 'My date'); $this->db->insert('mytable', $data); // Produces: INSERT INTO mytable (title, name, date) VALUES ('My title', 'My name', 'My date') $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

GlobAl CodESTER

Build SQL Update statement


$data = array('title' => $title,'name' => $name,'date' => $date); $this->db->where('id', $id); $this->db->update('mytable', $data);
// Produces: // UPDATE mytable SET title = '{$title}', name = '{$name}', date = '{$date}' WHERE id = $id

$data = array( array('title' => 'My title','name' => 'My Name 2' ,'date' => 'My date 2' ), array('title' => 'Another title' ,'name' => 'Another Name 2' ,'date' => 'Another date 2' ) ); $this->db->update_batch('mytable', $data, 'title');
// Produces: // UPDATE `mytable` SET `name` = CASE WHEN `title` = 'My title' 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') [

GlobAl CodESTER

Build SQL Delete statement


$this->db->delete('mytable', array('id' => $id)); // Produces: // DELETE FROM mytable WHERE id = $id $this->db->from('mytable'); $this->db->truncate(); // or // Produce: // TRUNCATE mytable

Note: If the TRUNCATE command isn't available, truncate() will execute as "DELETE FROM table".

$this->db->truncate('mytable');

GlobAl CodESTER

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 <html> <head> <title>My Blog</title> </head> Loading a View $this->load->view('name'); <body> <h1>Welcome to my Blog!</h1> </body> </html>

GlobAl CodESTER

Helper
Helpers means help you with tasks. Each helper file is simply a collection of functions or methods. Stored in your system/helpers, or application/helpers directory

Loading a Helper
$this->load->helper('name');

Few common Helpers are listed below:


URL Helpers, that assist in creating links Form Helpers that help you create form elements Text Helpers perform various text formatting routines Cookie Helpers set and read cookies File Helpers help you deal with files

GlobAl CodESTER

GlobAl CodESTER

Template Integration

Where we put css/js/image directory ?


Local CI Directory news_system Configure base_url & define one more variable BASE_URI
To do this, open up system/application/config/config.php

Directory structure of project news_system

GlobAl CodESTER

Create templates directory


Create a templates directory
To do this, open up application/views/

Create header.php & footer.php files in templates directory

GlobAl CodESTER

Link to css/js/images
For css directory <link rel="stylesheet" href="<?php echo BASE_URI; ?>css/layout.css" type="text/css" /> For js directory <script src="<?php echo BASE_URI; ?>js/jquery.js" ></script> For image directory <img src="<?php echo BASE_URI; ?>images/image.jpg" alt="text" />

GlobAl CodESTER

Testing of template integration

r ng o C

e nt i u yo , ion t a ul t a

t ra

ed

pl m te

su e t

es cc

sf

! ly l u

GlobAl CodESTER

GlobAl CodESTER

Template Engine

Template Engine
Template Parser Class enables you to parse pseudo-variables contained within your view files.

<html> <head>

Pse

ud o

-va

ria

ble

<title>{news_title}</title>

loo

k li

ke

Initializing the Class


Initialized in your controller $this->load->library(parser);

</head> <body> <h1>{news_heading}</h1> <p>{news_body}</p> </body> </html>

thi s

Pass data to view using $this->parser->parse()


Method accepts a template name and data array as input $data=array( news_title=>My News, news_heading=>My News heading); $this->parser->parse(news_template,$data);

The Template Parser Class is not a full-blown template parsing solution.

GlobAl CodESTER

GlobAl CodESTER

S-ar putea să vă placă și