0% found this document useful (0 votes)
20 views

Ch05 CodeIgniter Framework Notes

The document provides a comprehensive overview of the CodeIgniter framework, including its introduction, history, features, and software requirements. It explains the MVC architecture, application flow, directory structure, and how to create models, views, and controllers. Additionally, it outlines installation steps and includes references for further learning.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Ch05 CodeIgniter Framework Notes

The document provides a comprehensive overview of the CodeIgniter framework, including its introduction, history, features, and software requirements. It explains the MVC architecture, application flow, directory structure, and how to create models, views, and controllers. Additionally, it outlines installation steps and includes references for further learning.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

CodeIgniter Framework

By
Asst.Prof.Charushila Patil
Pratibha College of Commerce and
Computer Studies,Chinchwad
CodeIgniter framework by Mrs.Charushila Patil Date:19/04/2023 1
Topics to be covered…
• Intoduction to CodeIgniter
• History
• CodeIgniter features
• Software Requirement
• CodeIgniter URI
• MVC Architecture
• CodeIgniter Application flowchart
• File Structure
• Model,View,Controller
• Routing
• Helpers
• Adding /Loading JS and CSS
• Installation steps
• Hands on training with mini applications
CodeIgniter framework by Mrs.Charushila Patil 2
CodeIgniter Framework
• CodeIgniter is an Application Development Framework - a toolkit –
MVC framework that helps to structure the code and make redundant
task less tedious.
• Its goal is to enable you to develop projects much faster than you
could if you were writing code from scratch, by providing a rich set of
libraries for commonly needed tasks, as well as a simple interface and
logical structure to access these libraries.
• CodeIgniter lets you creatively focus on your project by minimizing
the amount of code needed for a given task.

CodeIgniter framework by Mrs.Charushila Patil 3


History
• CodeIgniter was originally developed by Rick Ellis (CEO of EllisLab,
Inc.). The framework was written for performance in the real world,
with many of the class libraries, helpers, and sub-systems borrowed
from the code-base of ExpressionEngine.
• It was, for years, developed and maintained by EllisLab, the
ExpressionEngine Development Team and a group of community
members called the Reactor Team.
• In 2014, CodeIgniter was acquired by the British Columbia Institute of
Technology and was then officially announced as a community-
maintained project.

CodeIgniter framework by Mrs.Charushila Patil 4


CodeIgniter Features
• A framework with a small footprint.
• Requires nearly zero configuration.
• Does not require you to use the command line.
• Extremely light weight, does not force any convention.
• Full featured database classes with support for several platforms
• Form and Data validation.
• Custom routing
• security
• clear, thorough documentation.

CodeIgniter framework by Mrs.Charushila Patil 5


Software Requirements
1. Apache Server
2. PHP version 5.6 or newer is recommended.
3. CodeIgniter framework
4. A database is required for most web application programming. Currently supported
databases are:
 MySQL (5.1+) via the mysql (deprecated), mysqli and pdo drivers
 Oracle via the oci8 and pdo drivers
 PostgreSQL via the postgre and pdo drivers
 MS SQL via the mssql, sqlsrv (version 2005 and above only) and pdo drivers
 SQLite via the sqlite (version 2), sqlite3 (version 3) and pdo drivers
 CUBRID via the cubrid and pdo drivers
 Interbase/Firebird via the ibase and pdo drivers
 ODBC via the odbc and pdo drivers

CodeIgniter framework by Mrs.Charushila Patil 6


CodeIgniter URLs
By default, URLs in CodeIgniter are designed to be search-engine and human friendly. Rather
than using the standard “query string” approach to URLs that
is synonymous with dynamic systems, CodeIgniter uses a segment-based approach:
example.com/news/article/ my_article
Note
Query string URLs can be optionally enabled, as described below.

URI Segments
The segments in the URL, in following with the Model-View-Controller approach, usually
represent:
example.com/class/function/ID
1.The first segment represents the controller class that should be invoked.
2.The second segment represents the class function, or method, that should be called.
3.The third, and any additional segments, represent the ID and any variables that will be passed
to the controller.

CodeIgniter framework by Mrs.Charushila Patil 7


CodeIgniter Application flowchart

CodeIgniter framework by Mrs.Charushila Patil 8


CodeIgniter Directory Structure
• CodeIgniter directory structure is divided into 3 folders:
• Application
As the name indicates the Application folder contains all the code of your
application that you are building. This is the folder where you will develop your
project.
• System
This folder contains CodeIgniter core codes, libraries, helpers and other files, which
help make the coding easy. These libraries and helpers are loaded and used in web
app development.
• User_guide
• This is your user guide to CodeIgniter. It is basically, the offline version of user
guide on CodeIgniter website

CodeIgniter framework by Mrs.Charushila Patil 9


CodeIgniter Directory Structure

CodeIgniter framework by Mrs.Charushila Patil 10


MVC Architecture

CodeIgniter framework by Mrs.Charushila Patil 11


Controllers
• Controllers are the heart of your application, as they determine how
HTTP requests should be handled. A Controller is simply a class file
that is named in a way that can be associated with a URI.
• Consider the url
• Example.com/index.php/blog/
• In the above example, CodeIgniter would attempt to find a controller
named Blog.php and load it.
• When a controller’s name matches the first segment of a URI, it will
be loaded.

CodeIgniter framework by Mrs.Charushila Patil 12


• Let’s create a simple controller so you can see it in action. Using your
text editor, create a file called Blog.php, and put the following code in
it:
<?php
class Blog extends CI_Controller
{
public function index()
{
echo 'Hello World!';
}
}
save the file to your application/controllers/ directory.
Note:-Your file should be save using classname.php with first letter in capital
CodeIgniter framework by Mrs.Charushila Patil 13
• The general syntax for calling the controller is as follows:
http://www.your-domain.com/index.php/controller/method-name
• Points to Remember:
• The name of the controller class must start with an uppercase letter.
• The controller must be called with lowercase letter.
• Do not use the same name of the method as your parent class, as it
will override parent class’s functionality

CodeIgniter framework by Mrs.Charushila Patil 14


To see the output

• Visit the url


• Example.com/index.php/blog
• You will get the output like:
• Hello world!

CodeIgniter framework by Mrs.Charushila Patil 15


Changing Default Controller name
• By default controller is welcome
• If you want to change the controller name
• Then go to Application->config->route.php file make the changes as
below
• $route[‘default controller’]=‘welcome’;
• $route[‘^(?|other|controller).*’]=‘welcome/$0’;

CodeIgniter framework by Mrs.Charushila Patil 16


Views
• This can be a simple or complex webpage, which can be called by the controller.
The webpage may contain header, footer, sidebar etc. View cannot be called
directly. Let us create a simple view. Create a new file under application/views
with name “test.php”.

CodeIgniter framework by Mrs.Charushila Patil 17


Loading the View
• The view can be loaded by the following syntax:
$this->load->view('name');
• Where name is the view file, which is being rendered. If you have
planned to store the view file in some directory then you can use the
following syntax:
• $this->load->view('directory-name/name');

CodeIgniter framework by Mrs.Charushila Patil 18


• The index() method is calling the view method and passing the “test”
as argument to view() method because we have stored the html
coding in “test.php” file under application/views/test.php.
<?php
class Test extends CI_Controller {
public function index()
{
$this->load->view('test');
}
}
?>

CodeIgniter framework by Mrs.Charushila Patil 19


Output is as below

CodeIgniter framework by Mrs.Charushila Patil 20


The following flowchart illustrates of how everything works:

CodeIgniter framework by Mrs.Charushila Patil 21


Models
• Models classes are designed to work with information in the
database. As an example, if you are using CodeIgniter to manage
users in your application then you must have model class, which
contains functions to insert, delete, update and retrieve your users’
data.

CodeIgniter framework by Mrs.Charushila Patil 22


Creating Model Class
• Model classes are stored in application/models directory. Following code shows
how to create model class in CodeIgniter.
<?php
Class Model_name extends CI_Model
{
Public function __construct()
{
parent::__construct();
}
}
?>
Where Model_name is the name of the model class that you want to give. Each
model class must inherit the CodeIgniter’s CI_Model class. The first letter of the
model class must be in capital letter.

CodeIgniter framework by Mrs.Charushila Patil 23


Loading Model
• Model can be called in controller. Following code can be used to load
any model.
• $this->load->model(‘ model_name');
• Where model_name is the name of the model to be loaded. After
loading the model you can
• simply call its method as shown below.
• $this->model_name->method();

CodeIgniter framework by Mrs.Charushila Patil 24


Helpers
• As the name suggests, it will help you build your system. It is divided
into small functions to serve different functionality. A number of
helpers are available in CodeIgniter, which are listed in the table
below. We can build our own helpers too.
• Helpers are typically stored in your system/helpers, or
application/helpers directory.
• Custom helpers are stored in application/helpers directory and
systems’ helpers are stored in system/helpers directory. CodeIgniter
will look first in your application/helpers directory. If the directory
does not exist or the specified helper is not located, CodeIgniter will
instead, look in your global system/helpers/ directory. Each helper,
whether it is custom or system helper, must be loaded before using it.

CodeIgniter framework by Mrs.Charushila Patil 25


CodeIgniter framework by Mrs.Charushila Patil 26
Adding /Loading JS and CSS
• To add JS and CSS files in CodeIgniter ,we have to create js and css folder in root
directory and copy all the .js files in js folder and .css files in CSS folder.

• To add this files in view.load URL helper in the controller as follows:


$this->load->helper(‘url’);

• Add the below code in the view file to load the .js and .css file as follows

<link rel=“stylesheet” type=“text/css” href=<?php echo base_url();?>css/style.css”>

<script type=“text/javascript” src=“<?php echo base_url();?>js/sample.js”></script>

CodeIgniter framework by Mrs.Charushila Patil 27


JS and CSS Example
• Create a controller called Example.php and save it in
application/controller/Example.php
<?php
Class Example extend CI_Controller
{
Public function index()
{
$this->load->helper(‘url’);
$this->load->view(‘example’);
}
}?>
CodeIgniter framework by Mrs.Charushila Patil 28
• Create a view file called example.php and save it in
application/view/example.php.
<html>
<head>
<title> codeIgniter view example</title>
<link rel=“stylesheet” type=“text/css” href=“<?php echo
base_url();?>css/style.css”>
<script type=“text/javascript” src=“<?php echo base_url();?>js/sample.js”></script>
</head>
<body>
<a href=‘javascript:test()’>Click here</a>Execute javascript function.
</body>
</html>

CodeIgniter framework by Mrs.Charushila Patil 29


Example continue…..
• create a CSS file called style.css and save it at css/style.css.
Body
{
Background:#FFF;
Color:#000;
}
• Create a JS file called sample.js and save it at js/sample.js.
Function test()
{
alert(‘test the function’);
}

CodeIgniter framework by Mrs.Charushila Patil 30


CodeIgniter installation steps:

• Step-1: Download the CodeIgniter from the link


http://www.codeigniter.com/download
• Step-2: Unzip the folder.
• Step-3: Upload all files and folders to your server.
• Step-4: After uploading all the files to your server, visit the URL of
your server, e.g., www.domain-name.com.

CodeIgniter framework by Mrs.Charushila Patil 31


On visiting the URL, you will see the following screen:

CodeIgniter framework by Mrs.Charushila Patil 32


References
Ref.Links
1. https://codeigniter.com
2. https://www.tutorialspoint.com/codeigniter/codeigniter_tutorial.pdf
3. https://www.phptpoint.com

Ref.Books
1.Professional Codeigniter By Thomas Myer ,Wrox Publication
2. Codeihniter 2 CookBook By Rob Foster ,PACKT Publication

CodeIgniter framework by Mrs.Charushila Patil 33


thank you !

CodeIgniter framework by Mrs.Charushila Patil 34

You might also like