The Wayback Machine - https://web.archive.org/web/20240619194915/https://www.geeksforgeeks.org/introduction-to-codeignitor-php/
Open In App

Introduction to Codeignitor (PHP)

Last Updated : 31 May, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

Codeignitor is one of the popular MVC framework of PHP. Most of the developers prefer to make their projects on Codeignitor because of it’s lightweight and easy to understand documentation. Some of the features, advantages or why use Codeignitor is given below.

Why use Codeignitor?

  1. Fast and lightweight because it’s library consume less memory.
  2. Easy to learn to make project of medium level.
  3. Clear documentation to understand everything easily and build the project on your own
  4. Easy error handling, easy to debug and deal with security issues.
  5. Large support of community and developers are available worldwide.
  6. Paging support, encryption support, file uploading class available, inbuilt class for sending email.

Downloading Codeignitor: Download the Codeignitor and save it in your htdocs folder with your project name suppose ci_tutorial. Then run this project in your localhost with url http://localhost/ci_tutorial . Below is the result once codeignitor will be installed and run successfully on localhost.

Directory Structure: There are mainly three folders in the directory structure of Codignitor. These are the application, system, and user_guide. Most of the work will be done in the application folder where model, view, controller folder will be available along with other folders. Below is the image of the directory structure.

Below is the structure of application folder where most of the task developer perform.

How it Works: The main file of codeignitor is index.php so if you run http://localhost/ci_tutorial/index.php in the browser you will get the same view result of ‘Welcome to Codeignitor’ . Now open the route.php which is in configuration folder. There you will find default controller name in the end of the file.

$route['default_controller'] = 'welcome';

The above code defines that by default CI is running welcome controller and that file is available in the Controller folder with Welcome.php name. You can change the default controller name from here. Once you will open Welcome.php controller in Controller folder you will find index function which is by default function of Welcome controller.

public function index()
{
     $this->load->view('welcome_message');
}

The above index function is loading ‘welcome_message’ view file from view folder. So the whole concept is, routing tells which one is default controller, then hit that controller and then the index function(by default) of that controller will view the file which is defined in the function.
Index function in any controller is the by default function. To run any other controller or a function(apart from index function) in a controller you can define it manually in URL after creating the controller and its function in the folder. Let’s see how to do that.
Create a controller Test.php (Controller name should start in upper case) in your controller folder and write down the code below here.




<?php
defined('BASEPATH') OR exit('No direct script access allowed');
  
class Test extends CI_Controller {
    public function index()
    {
        $this->load->view('test_message');
    }
      
    public function test_demo()
    {
        $this->load->view('test_tutorial');
    }
}
?>


Now create a views file test_message.php, write down the code below and save this in your view folder.




<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?>
<html lang="en">
<body>
    <h3>Welcome to GeeksforGeeks</h3>
</body>
</html>


Now in your browser type this url http://localhost/ci_tutorial/index.php/Test. You will find below result
Output:

So here in the URL define the controller name after index.php which will run default method index.php from Test controller.
Now create a test_tutorial.php file in view folder and write down the code below.




<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?>
<html lang="en">
<body>
 <h3>This is the test tutorial</h3>
</body>
</html>


Now run the url http://localhost/ci_tutorial/index.php/Test/test_demo in your browser and below is the result.
Output:

So here to run any other function from a controller all need to do is to define the controller name then function name in URL after index.php.



Previous Article
Next Article

Similar Reads

How MVC works in Codeignitor ?
Codeignitor is based on MVC design pattern. We will see how the controller, model, and view work and display the result to the user. Controller: Like the name suggest, it controls the communication between views and models. All the URL works in CodeIgniter with controller name. Now let's see some point which we need to keep in mind when we create a
4 min read
Helpers in Codeignitor
Helpers are the reusable code in codeignitor like libraries. The only difference is that libraries are collection of classes whereas helper is defined as individual independent set of functions. Helper functions need to be loaded before using it. We can find all the helpers in codeignitor documentation Codeignitor Helpers and that can be used depen
4 min read
Libraries in Codeignitor
Any kind of framework is a collection of reusable code which is divided into classes, so if we talk about OOPS or MVC, we do all our task in classes like for email sending, form validation or pagination kind of work. Collection of these classes are called a library in Codeignitor. There are so many libraries in Codeignitor which can find in it's do
5 min read
PHP | MySQL Database Introduction
What is MySQL? MySQL is an open-source relational database management system (RDBMS). It is the most popular database system used with PHP. MySQL is developed, distributed, and supported by Oracle Corporation. The data in a MySQL database are stored in tables which consists of columns and rows.MySQL is a database system that runs on a server.MySQL
4 min read
PHP | Spreadsheet_Excel_Writer | Introduction
Introduction: Spreadsheet_Excel_Writer is a library written in PHP which helps to read from and write to different types of spreadsheet file formats with the help of given set of classes. Advantages: Easy and effective comparisons. Powerful analysis of large amounts of data. Splitting out large (or not so large) amounts of information in the form o
1 min read
PHP Introduction
The term PHP is an acronym for - Hypertext Preprocessor. PHP is a server-side scripting language designed specifically for web development. It is open-source which means it is free to download and use. It is very simple to learn and use. The file extension of PHP is ".php". PHP was introduced by Rasmus Lerdorf in the first version and participated
6 min read
PHP 5 vs PHP 7
PHP is a server side scripting language designed for web development by Rasmus Lerdorf in 1994. Since its launch in 1994 PHP has become an industry standard supporting almost 80% of the websites ( 79.8% to be precise) with its closest competitor being ASP.Net at 19.8% and others like Ruby, Java trailing far behind. The PHP development team released
4 min read
PHP | Get PHP configuration information using phpinfo()
PHP provides us with a built-in function phpinfo() which gives us the details about the PHP version and PHP configuration of PHP installed in our system. To know about the Configurations and PHP version which is installed in your computer, a simple PHP script can be used. The script consists of a PHP function called "phpinfo()" which outputs inform
2 min read
PHP | php.ini File Configuration
At the time of PHP installation, php.ini is a special file provided as a default configuration file. It's very essential configuration file which controls, what a user can or cannot do with the website. Each time PHP is initialized, the php.ini file is read by the system. Sometimes you need to change the behavior of PHP at runtime, then this config
5 min read
How to import config.php file in a PHP script ?
The include statement in PHP copies the code of text from the file mentioned, into the file that uses the include statement. It directs the preprocessor to insert the content specified into the following program. The name of the file to be included is written in double-quotes. It is a good practice to write the basic database details and user detai
2 min read