Basic of Codeigniter

download Basic of  Codeigniter

of 33

Transcript of Basic of Codeigniter

  • 8/22/2019 Basic of Codeigniter

    1/33

    Speaker:

    PANKA

    [GlobAl

  • 8/22/2019 Basic of Codeigniter

    2/33

    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

    [ Gl

  • 8/22/2019 Basic of Codeigniter

    3/33

    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

    [ Gl

  • 8/22/2019 Basic of Codeigniter

    4/33

    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, a

    Application security is a focus

    Easy caching operations

    Many libraries and helpers to help you with complex operations such as email, image manipulation,fo

    Most libraries are only loaded when needed which cuts back on resources needed

    [ Gl

  • 8/22/2019 Basic of Codeigniter

    5/33

    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.

    [ Gl

  • 8/22/2019 Basic of Codeigniter

    6/33

    Routing Basics

    [ Gl

  • 8/22/2019 Basic of Codeigniter

    7/33

    Installation

    and

    Configuratio

    [ GlobAl Co

  • 8/22/2019 Basic of Codeigniter

    8/33

    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.

    [ Gl

  • 8/22/2019 Basic of Codeigniter

    9/33

    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 relea

    your existing application just by replacing this system directory with the latest release.

    The user_guidehouses the user guide to CI.

    The index.phpfile is the bit that does all the CI magic.

    [ Gl

  • 8/22/2019 Basic of Codeigniter

    10/33

    The cachefolder stores all the caches generated by the caching library.

    This configdirectory includes settings/configuration related information like database settings, route i

    The core directoryneeded 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

    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 mu

    The libraries folder stores all the libraries which are specific to your application. The third_partydirectory will include library files, but only those, which are imported from third-party

    Application Directory Structure

    [ Gl

  • 8/22/2019 Basic of Codeigniter

    11/33

    The database folder stores all the database drivers and class which enable you to connect to database

    The fontsfolder stores all the fonts which can be used by the image manipulation library.

    The helpersfolder stores all of CIs core helpers but you can place your own helpers in here which can

    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.

    System Directory Structure

    [ Gl

  • 8/22/2019 Basic of Codeigniter

    12/33

    Configuration

    Need to set upbase_url.

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

    Need to set up databaseTo use a database, open up system/application/config/database.php

    Add database to the autoloTo do this, open up system/app

    [ Gl

  • 8/22/2019 Basic of Codeigniter

    13/33

    Testing CodeIgniter

    Cong

    ratul

    ation

    ,youc

    onfig

    uredCod

    eIgnit

    ersu

    c

    [ Gl

  • 8/22/2019 Basic of Codeigniter

    14/33

    Model

    View

    Controller

    [ GlobAl Co

  • 8/22/2019 Basic of Codeigniter

    15/33

    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/newsHere CI will find news.phpController

    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');

    }

    }

    Let's create a simple news controller so you can see it in action. Then save the file news.phpto applicatio

    Class names must start with an

    uppercase letter.

    Important Note:

    [ Gl

  • 8/22/2019 Basic of Codeigniter

    16/33

    Passing URI segments to function

    http://localhost/news_system/index.php/news/view/initializing-the-class

    class News extend

    public fu

    {

    }

    }

    CI allow to pass more than one segments

    function name

    Class ConstructorsIf you intend to use a constructor in Controllers, MUSTplace

    parent::__construct();

    Constructors are useful if you need to set some default values, or run

    a default process when your class is instantiated.

    [ Gl

  • 8/22/2019 Basic of Codeigniter

    17/33

    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

    File name will be lower case version

    of class name i.e news_model.php

    ImportantNote

    Loading a Model

    $this->load->model('Model_name');

    Use a Model

    $this->Model_name

    [ Gl

  • 8/22/2019 Basic of Codeigniter

    18/33

    Active Record Class

    CodeIgniter uses a modified version of the Active Record Database Pattern.

    Pattern allows information to be retrieved, inserted, and updatedin your database with minimal scrip

    A major benefit to using the Active Recordfeatures is that it allows you to create database independe

    [ Gl

  • 8/22/2019 Basic of Codeigniter

    19/33

    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

    [ Gl

  • 8/22/2019 Basic of Codeigniter

    20/33

    $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')

    Build SQL Select statement

    [ Gl

  • 8/22/2019 Basic of Codeigniter

    21/33

    $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

    Build SQL Insert statement

    [ Gl

  • 8/22/2019 Basic of Codeigniter

    22/33

    $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 'An

    `name` END, `date` = CASE WHEN `title` = 'My title' THEN 'My date 2' WHEN `title` = 'Another title' THEN 'Another date 2'

    `title` IN ('My title','Another title')

    Build SQL Update statement

    [ Gl

  • 8/22/2019 Basic of Codeigniter

    23/33

    $this->db->delete('mytable', array('id' => $id));

    // Produces:// DELETE FROM mytable WHERE id = $id

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

    $this->db->truncate();

    // or

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

    // Produce:

    // TRUNCATE mytable

    Build SQL Delete statement

    Note: If the TRUNCATE command isn't

    available, truncate() will execute as "DELETE

    FROM table".

    [ Gl

  • 8/22/2019 Basic of Codeigniter

    24/33

    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

    Loading a View

    $this->load->view('name');

    My Blog

    Welcome to

    [ Gl

  • 8/22/2019 Basic of Codeigniter

    25/33

    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

    Few common Helpers are listed below:

    URL Helpers, that assist in creating links

    Form Helpersthat help you create form elements

    Text Helpersperform various text formatting routines

    Cookie Helpersset and read cookies

    File Helpershelp you deal with files

    Loading a Helper

    $this->load->he

    [ Gl

    [ GlobAl Co

  • 8/22/2019 Basic of Codeigniter

    26/33

    Template Integra

  • 8/22/2019 Basic of Codeigniter

    27/33

    Where we put css/js/imagedirectory ?

    Local CI Directory news_system

    Configure base_url& define one more variable BASE_URITo do this, open up system/application/config/config.php

    Directory structure of project news_system

    [ Gl

  • 8/22/2019 Basic of Codeigniter

    28/33

    Create templates directory

    Create atemplatesdirectoryTo do this, open up application/views/

    Create header.php& footer.phpfiles in templates directory

    [ Gl

  • 8/22/2019 Basic of Codeigniter

    29/33

    Link to css/js/images

    For cssdirectory

  • 8/22/2019 Basic of Codeigniter

    30/33

    Testing of template integration

    Cong

    ratul

    ation

    ,youi

    nteg

    ratedt

    empla

    tesu

    c

    [ Gl

    [ GlobAl Co

  • 8/22/2019 Basic of Codeigniter

    31/33

    Template Engi

  • 8/22/2019 Basic of Codeigniter

    32/33

    Template Engine

    Template Parser Class enables you to parse pseudo-variables contained within

    your view files.

    Initializing the Class

    Initialized in your controller

    $this->load->library(parser);

    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

    template parsing solution.

    {news_title}

  • 8/22/2019 Basic of Codeigniter

    33/33

    [ Gl