CodeIgniter Tutorial
CodeIgniter tutorial provides basic and advanced concepts of CodeIgniter. Our CodeIgniter tutorial is designed for beginners and professionals.
Our CodeIgniter tutorial includes all topics of such as CodeIgniter architecture, versions, models, file system, url, Model, View, Controller, database configuration, save record, view record, delete record, update record, crud example, authentication example etc.
Prerequisite
Before learning CodeIgniter you must have the basic knowledge of PHP.
Audience
Our CodeIgniter tutorial is designed to help beginners and professionals.
Problem
We assure that you will not find any problem in this CodeIgniter tutorial. But if there is any mistake, please post the problem in contact form.
What is CodeIgniter
For building a web application you spend a lot of time in writing the same code again and again. Frameworks provide you a starting block and minimize the amount of code needed to build a website.
CodeIgniter is PHP driven framework but it's not a PHP substitute. Diving into CodeIgniter doesn?t mean you are leaving PHP behind. PHP is a server-side scripting language for building dynamic web-based applications.
CodeIgniter contains libraries, simple interface and logical structure to access these libraries, plug-ins, helpers and some other resources which solve the complex functions of PHP more easily maintaining a high performance. It simplifies the PHP code and brings out a fully interactive, dynamic website at a much shorter time. It supports PHP version of 5.2.6 or newer and MySQL version 4.1 or newer. It makes your web more robust and your code easier to read and maintain. It is a free toolkit, light weight and easier to install.
A person using CodeIgniter must be familiar with PHP. You need to have a good knowledge about PHP like its basic syntax and how it interacts with database and HTML.
Why you should use CodeIgniter
- If you need a framework with small footprint.
- You need a high performance.
- Need a framework which requires zero configurations.
- Need a framework which don't use command line.
- Need a framework which doesn't require adhering to restrictive coding rules.
- To get a simplified code structure.
What is CodeIgniter
For building a web application you spend a lot of time in writing the same code again and again. Frameworks provide you a starting block and minimize the amount of code needed to build a website.
CodeIgniter is PHP driven framework but it's not a PHP substitute. Diving into CodeIgniter doesn?t mean you are leaving PHP behind. PHP is a server-side scripting language for building dynamic web-based applications.
CodeIgniter contains libraries, simple interface and logical structure to access these libraries, plug-ins, helpers and some other resources which solve the complex functions of PHP more easily maintaining a high performance. It simplifies the PHP code and brings out a fully interactive, dynamic website at a much shorter time. It supports PHP version of 5.2.6 or newer and MySQL version 4.1 or newer. It makes your web more robust and your code easier to read and maintain. It is a free toolkit, light weight and easier to install.
A person using CodeIgniter must be familiar with PHP. You need to have a good knowledge about PHP like its basic syntax and how it interacts with database and HTML.
Why you should use CodeIgniter
- If you need a framework with small footprint.
- You need a high performance.
- Need a framework which requires zero configurations.
- Need a framework which don't use command line.
- Need a framework which doesn't require adhering to restrictive coding rules.
- To get a simplified code structure.
CodeIgniter License
CodeIgniter is open source software, licensed under MIT License and its source code is maintained at GitHub.
As it is open source software, you are permitted to copy, modify and distribute this software and its documentation for any purpose under following conditions.
- Redistributed source code must retain the copyright notice.
- Modified files must state the changes made and carry name of those who changed them.
- Derived products cannot be named same as 'CodeIgniter' without written permission from British Columbia Institute of Technology.
- All distributions must include a copy file of this agreement.
- An acknowledgement must be included with derived products that they are derived from CodeIgniter.
License Agreement
Copyright (c) 2008-2014, EllisLab, Inc.
Copyright (c) 2014-2015, British Columbia Institute of Technology
CodeIgniter Versions
- v3.1.0 (this is the current version)
- v3.0.6
- v3.0.5
- v3.0.4
- v3.0.3
- v3.0.2
- v3.0.1
- v3.0.0
- v2.2.3
- v2.2.3
- v2.2.2
- v2.2.1
- v2.1.4
- v2.1.3
- v2.1.2
- v2.1.1
- v2.1.0
GitHub
GitHub is a web based hosting service. It offers distributed version control and source code management functionality. CodeIgniter source code is maintained at GitHub.
Features of CodeIgniter
There is a huge demand for CodeIgniter framework in PHP developers due to its versatile features and advantages. A web application developed on CodeIgniter performs effectively and rapidly. It provides an advanced set of aspects to write from scratch to build a dynamic web application.
Important Features
- Free to use It is licensed under MIT license, so it is free to use.
- Follows MVC Pattern It uses Model-View-Controller which basically separates logic and presentation parts. Request comes to controller, database action is performed through model and output is displayed through views.
- Light weight It is extremely light-weighted. CodeIgniter core system requires very small library, other libraries may be added upon dynamic request based upon your needs. That is why it is quite fast and light weighted.
- Generate SEO friendly URLs URLs generated by CodeIgniter are search-engine friendly and clean. It uses a segment based approach rather than standard query based approach.
- Built-in libraries It comes with full packet libraries that enable all the web needed tasks like database, form validation, sending email, manipulating images, sending emails, etc.
But in normal PHP scripting, every page represents MVC which increases complexity.
Some other Features
- Security and XSS Filtering
- File uploading, session management, pagination, data encryption
- Flexible URI Routing
- Zip encoding class
- Error logging
- Full page caching
- Localization
CodeIgniter Installation
Follow given steps to install CodeIgniter:
1) Download CodeIgniter from its official website.
Download current version of CodeIgniter from its official website
https://www.codeigniter.com
2) Unzip CodeIgniter package.
Downloaded CodeIgniter will be in zip format. Copy it and place it in your htdocs folder. Unzip and rename it. We are naming it as CodeIgniter.
3) CodeIgniter user guide
On browser type localhost/CodeIgniter/ (after localhost type name of your unzipped folder). If the above snapshot page appears then it means your file is successfully installed.
4) Set the base URL in application/config/config.php file with any text editor.
5) You need to establish the connectivity to your database. Go to the path application/config/database.php file.
Look at the above snapshot, fill the details about your database like hostname, username, password and database name.
File structure in CodeIgniter
After unzipping the CodeIgniter folder you will get a file hierarchy of CodeIgniter files as shown below.
CodeIgniter file structure is mainly divided into three parts:
- Application
- System
- User_guide
Application
Application folder is the main development folder for you where you will develop your project. It contains your models, views, controllers, configuration and many other files. It contains all the code of the project you are working on.
- Cache - Cache stores the processed data so that this data can be easily loaded within no time for the future use. It increases the speed of your page access.
- Config - The config folder contains configuration files as shown below. These files allow configuring CodeIgniter application.
Look at the above snapshot, autoload.php file will load your libraries, helpers or you can define custom files so you don't have to call them again and again in your project. In config.php file we set our base-url etc. In database.php file we need to configure our database setting to connect it from our project. In routes.php file you can set your default controller page.
- Controllers - Web application flow is controlled by the controller. All the server-side functionalities are handled by the controller. In short, it controls the CodeIgniter application. If controller fails all the work associated with it will also fail, just like CPU in a computer. Name of the controller class file will always start with an uppercase letter. For example, it will be named like Main.php and not main.php.
- Core - CodeIgniter has some core class, these class make up the CodeIgniter framework and are saved in core file. Generally, there will be no need to change these classes, but in case if you are modifying a class, create a class in "application/core" folder having same name as the core class file name in "system" folder.
- Helpers - A helper helps you to complete task in CodeIgniter. For example, $this->load->helper('form'); will create a form that will work perfectly with CodeIgniter. And
- Hooks - This folder meddles into the inner working of flow of application.
- Language - With the help of this folder you can create text files with specific language and can use them in your project.
- Libraries - In this folder you can store libraries developed by you for your application.
- Logs - If your CodeIgniter application is displaying some error or exception handling messages and if you are not getting what they are, you can look for their explanation in this folder.
- Models - Models are used to load database queries. Controllers request model to load database query, model respond it back and then controller use it.
- Third_party - Third party plugins are stored in this folder to use in the application.
- Views - It contains all your html files. Controller load file from view and then gives the output.
$this->load->helper('date'); will get you date features in your applications.
System
All action of CodeIgniter application happens here. It contains files which makes the coding easy.
- Core - It contains CodeIgniter core class. Do not make any changes in this folder.
- Database - It contains database drivers and other utilities.
- Fonts - It contains font related information.
- Helpers - It contains default helpers such as URL, date and cookie.
- Language - CodeIgniter supports multilingual web applications. It contains default language file.
- Libraries - It contain libraries like calendars, file upload, email, etc. libraries created by you will be saved in "application/libraries". Here, only standard libraries will be stored.
User_guide
It is the offline CodeIgniter guide. It comes with every CodeIgniter downloaded version. In case of any query, you can read its user guide. You can learn here all the functions, libraries, helpers of CodeIgniter. Before starting use of CodeIgniter go through this guide once.
Last but not least, you can see a file index.php. Here we can set application environment and error level. It is better not to touch this file if don?t have sufficient knowledge.
CodeIgniter Architecture
CodeIgniter is designed to deliver maximum performance in less time within a clean environment. To achieve this, each developing process is designed in a simplified way.
From technical point of view it is dynamically instantiation (libraries are loaded on request which makes it light-weighted), loose coupling. (components rely very less on each other) and component singularity (each class and its functions are narrowly focused only towards their purpose).
Data flow in CodeIgniter
Look at the above snapshot, this flow chart displays data flow in CodeIgniter.
- File index.php is the default file of CodeIgniter. It initializes the base resources.
- The Router decides what should be done with the information.
- If requested cache file exists, then the information is passed directly to the browser ignoring the further processes.
- Before loading Application Controller, the HTTP request and submitted data is passed under Security check.
- The Application Controller loads Models, Libraries, Helpers, Plugins and Scripts needed according to the request.
- The final page will come to View and then sent to the web browser. If View page is not cached then it will be cached first for future requests.
Model-View-Controller (MVC)
CodeIgniter framework is based on MVC pattern. MVC is a software that gives you a separate logical view from the presentation view. Due to this, a web page contains minimal scripting.
Model
Models are managed by the Controller. It represents your data structure. Model classes contain functions through which you can insert, retrieve or update information in your database.
Some points to be noted
By default, index method is always loaded if you have not written any second method in the URL. For example, if your method is
Then your URL will be like
OR
But if your method is
Then your URL will be like
View
View is the information that is presented in front of users. It can be a web page or parts of page like header and footer.
Controller
Controller is the intermediary between models and view to process HTTP request and generates a web page. All the requests received by the controller are passed on to models and view to process the information. It is the center of your every request on your web application.
Some points to be noted
- The Controller file must be named starting with the uppercase letter.
- Class name should also start with the uppercase letter and should be the same as your file name.
- The given calss extends to the CI_Controller so that it inherits all its methods.
If you think that Models are of no use to you or they are more complex then you can ignore them and build your project using Controllers and views.
Models
What is a Model
In any application you need to call a function to retrieve some information from the database. Models responsibility is to handle all data logic and representation and load data in the views. It is stored in application/models.
Look at the above snapshot, this is the basic structure of a model file.
Here, ModelName is the name of your model file. Remember, class first letter must be in uppercase letter followed by other lowercase letters and it should be same as your file name. It extends the base CodeIgniter Model so that all the built in methods of parent Model file gets inherited to the newly created file.
Model file name will be saved with an uppercase letter in the folder application/models. For example, if the above Modelname is your class then your file name will be ModelName.php
Loading a Model
Models are loaded in the conntroller's file with the following code,
If in case your model file is located in sub-directory of model folder, then you have to mention the full path. For example, if your file location is application/controller/models/project/ModelName. Then, your file will be loaded as shown below,
Connecting Models to Database
Loading a model doesn't mean it will automatically connect to your database. There are different methods to connect a database.
.Auto-connect feature will automatically load your database with every page load. To enable it, add word 'database' in the array library in autoload.php file.
Manually connect database by adding this code in the page where needed.
No comments:
Post a Comment