Documente Academic
Documente Profesional
Documente Cultură
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
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
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
GlobAl CodESTER
GlobAl CodESTER
Configuration
Need to set up base_url.
To do this, open up system/application/config/config.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
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
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(); } }
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
GlobAl CodESTER
GlobAl CodESTER
GlobAl CodESTER
GlobAl CodESTER
$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
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');
GlobAl CodESTER
GlobAl CodESTER
Template Integration
GlobAl CodESTER
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
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
thi s
GlobAl CodESTER
GlobAl CodESTER