Thursday, December 26, 2024
Google search engine
HomeLanguagesIntroduction to Codeignitor (PHP)

Introduction to Codeignitor (PHP)

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 neveropen</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.

RELATED ARTICLES

Most Popular

Recent Comments