Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Code Igniter

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

CodeIgniter – Overview

CodeIgniter is a simple , elegant and powerful toolkit with a very small footprint, used by those
developers who want to create full-featured Web Applications. CodeIgniter is an Open Source
PHP Framework. It has a very rich set of functionality, which will increase the speed of website
development work.

As there are various sources through which websites can be developed, but Codeigniter is
preferred over the others

CodeIgniter Features
Some of the most important features are mentioned below:

 CodeIgniter is very simple to configure, as it is an open source framework.


 As per our own requirement we can do customization very easily.
 Those who don’t want to waste a lot of time in difficult coding, as coding in PHP is easy to
use, simple and very quick.
 It allows us to well organize the code underlying our webpage easily.
 The folder structure used here is linear therefore very easy to use.
 It helps in easy hassle- free migration of server hosting from one to another.
 CodeIgniter make use of the MVC system to simplify the coding, expedient and reusable.
 It helps to find out the errors in codes and fix the issues in the web applications.
 It is a user-friendly which help developers to create a dynamic, secure and effective web
applications in a short time.
 Programmers can create web applications as with additional features and high-end
functionalities by using in-built resource and libraries of the CodeIgniter.
 Programmers uses CI because of Fast development, It creates both front-end and rear-end of
a web-application in secure and fast manner.
 Its active record implementation feature is outstanding and quick and easy to remember.
CodeIgniter – Installing
Codeigniter installation is a very easy process. For the installation please follow the given below
steps:

 Step 1 : Click at the link to download the


Codingniter:https://www.codeigniter.com/download
o Codeigniter comes in three flavors: CodeIgniter 3 (Current), CodeIgniter 4 (Future) and
CodeIgniter 2 (Legacy). For more details follow the link mentioned above.

o We can also get all the latest scripts with GitHub.

 Step 2 : Unzip the folder


 Step 3 :Upload all the files and folders to your server.
 Step 4 :After following step 3, visit the URL of your server, e.g. www.domain–name.com

CodeIgniter Application Architecture

The working of Codeigniter Application is mentioned in a simple flowchart given below, which
will help you understand the entire process effortlessly in easy steps. Each and every step in the
flow chart is explained in elaboration and point wise for your easy grasping.
 As shown in the Flow chart, whenever a request comes to CodeIgniter, it
will first go to index.php page.
 In the second step, Routing decides whether to pass the request to step
3 for Caching or to pass the request to step 4 for Security check.
 If the requested page is already in Caching, then Routing will sanction
the request to step 3 and the response will go back to the user.
 In case the request page does not exist in the Caching, then Routing,
will sanction the requested page to step 4 for Security checks.
 Before passing the request to Application Controller, the Security of
the submitted data is checked. After the Security check is done, the
Application Controller loads all the necessary Models, Libraries,
Helpers, Plugins and Scripts and pass it onto View.
 The View will provide the page with available data and pass that on
for Caching, to process this page quickly for future requests.

Directory Structure of Codeigniter


The Directory structure of the Codeigniter is given below in the Screenshot Image:

Further to this the Directory Structure of CodeIgniter is divided into three folders:

 Application
 System
 User_Guide

Application
As indicated in the name the Application folder contains all the code of your application that you
are building. This folder is the actual place where we develop the project. Within this application
folder there exist several other folders as explained below:

 Cache – The Cache folder contains all the cached pages of your application.
These cached pages would increase the overall speed of accessing the pages.
 Config – The Config folder contains various files to configure the application.
With the help of config.php file, you can configure the application. Using
database.php file, you can configure the database of your application.
 Controllers – This folder contains the controllers of your application. It is the
basic part of your application.
 Core – This folder contains the base class of your application.
 Helpers – The helper class of your application can be put in this folder.
 Hooks – With the help of files in this folder you can tap into and modify the
inner workings of the framework without hacking the core files.
 Libraries – This folder contains the files of the library developed for your
application.
 Logs – This folder contains the files related to the log of the system.
 Language –This folder contains language related files.
 Models – The database login is placed in this file.
 Third_Party – The plugins can be placed in this folder for using it in your
application.
 Views – The HTML files of your application can be placed here.
System:
The System folder contains the CodeIgniter core codes, libraries, helpers and other files, which
helps make the coding easy. The libraries and helpers are loaded and used for web application
development. This folder contains all the CodeIgniter code of consequence again organized into
various folders mentioned as below:

 Core – The Core folder contains CodeIgniter core class. Nothing should be
modified here. Since all your work will take place in the application folder, if
you intend you extend the CodeIgniter core you will have to do it with hooks,
which itself exist in the application folder.
 Database – This folder contains the database drivers and database utilities.
 Fonts – This folder contains the font related information and the utilities.
 Helpers – The standard CodeIgniter helpers such as data, cookie, and URL
helpers exist in this folder.
 Language –Contains language related files. This can be ignored for now.
 Libraries – The folder contains standard CodeIgniter libraries such as e-mail,
calendars, file uploads etc. You can create your own libraries, extend and
even replace standard ones. But those will be saved in the application/
libraries directory to keep them separate from the standard CodeIgniter
libraries in this particular folder.

User_Guide:
It is the offline version of user guide of CodeIgniter Website. In other words user guide to
CodeIgniter. Using this the various functions of libraries, classes and helpers can be learnt. It is
highly recommended that you go through this user guide before building your first web app
using CodeIgniter.

Apart from these three folders ( Application, System and User_Guide ) there is one more
important file named “ index.php” . In this file you can set the application environment and error
level and you can define system and application folder name. We highly recommend you not to
edit these settings if you do not have enough knowledge of what you are going to do.
CodeIgniter – MVC Framework
Codeigniter is PHP Framework which is built on the top of the MVC (Model-View-Controller)
development pattern. Model-View-Controller is a software approach which separates
application logic from presentation. Since the presentation is separate from the PHP scripting
here, it permits the web pages to contain minimal scripting.

 Model- The Model consists of the data structures. In a way These typically contains the sets
of the functions that helps in retrieving, inserting and updating information in the database.
 View- The view is the information that is being presented to the user. A view in normal case
is a webpage, but in case of CodeIgniter, it can be a page fragment like header or footer, or it
can be any other type of page like RSS as well as for performing UI.
 Controller- The controller implements the logical implementation for writing the program
and creating the webpage. The controller serves as an intermediary between the model and
the view and any other resources needed to process HTTP request and generate a webpage.
Controller
The controller implements the logical implementation for writing the programme and creating
the webpage. The controller serves as an intermediary between the model and the view.
The Controller Controls Model as well as view.

Creating a Controller
Controller always stored inside Application/controllers folder.Condeigniter provides two basic
files that is stored inside Application/controllers folder index.html and Welcome.php. Keep
these files as they are don’t delete these files.

Now Create a new file under the same path(Application/controllers) named “Hello.php”

1 <?php
2 class Hello extends CI_Controller
3 {
4 public function index()
5 {
6 echo "Hello World";
7 }
8 }
9 ?>

Save Hello class


inside Application/controllers/Hello.php.
Here, Hello class inherit an in-built Codeigniter Class CI_Controller.

Points to Remember
 CI_Controller class must be extended Whenever u want to create Your own Controller.
 Controller name must be Started with Capital letter
 Controller name and file name must be Similar

Calling a Controller
Syntax

1 http://localhost/index.php/controller-name/method-name

Open Your Web browser and type inside


“http://localhost/CodeIgniter/index.php/Hello” OR “http://localhost/CodeIgniter/index.php/
Hello/index”
Explanation: Here Hello is Controller name and index is method name.
By default index method automatically calls so if u want to call then type inside url
otherwise leave method name.
Output

Creating Hello Controller with 2 methods index


and about
1 <?php
2 class Hello extends CI_Controller
3 {
4 public function index()
5 {
6 echo "Hello World";
7 }
8 public function about()
9 {
10 echo "About us";
11 }
12 }
13 ?>

Calling Hello Controller’s index method


1 http://localhost/CodeIgniter/index.php/Hello/index

Output
Hello World

Calling Hello Controller’s about method


1 http://localhost/CodeIgniter/index.php/Hello/about

Output
About us

Views
The view is a simple webpage(designing page) which called by Controller.
The view may contain Navbar, Header, Footer, Sidebar, slider etc.
The view(designing page) can’t access directly it always called by Controller.
Lets create a simple view page and call on a Controller’s method.
Ex

1 <!DOCTYPE html>
2 <html>
3 <head>
4 <title>About CI View</title>
5 </head>
6
7 <body>
8 <h1>About Codeigniter Views</h1>
9 </body>
10 </html>

Save this file inside Application/views/about.php.

loading view on a controller


The view can be loaded on a controller following syntax :
$this->load->view('view_page_name');
Sometimes we can create different directly like user, admin etc. inside user directory we
may store header, footer,sidebar for users
same as admin inside admin directory, in this case we can call like :
$this->load->view('directory_name/view_page_name');

Calling view on a Controller


Add a new mehtod about() on Hello Controller Application/controllers/Hello.php.

1 <?php
2 class Hello extends CI_Controller
3 {
4 public function index()
5 {
6 echo "Hello World";
7 }
8 public function about()
9 {
10 $this->load->view('about');
11 }
12
13 }
14 ?>

Here Hello Controller has now 2 methods. One is index() that contains “Hello World” message
only.
Second method about() is calling about.php view page.
Note : inside Controller no need to call page extension of view.
Calling about() method of Hello Controller
Syntax

http://localhost/index.php/hello/about

You might also like