CodeIgniter Interview Questions and Answers

Questions

40

Last updated

Mar 24, 2024

CodeIgniter is a PHP open-source web development framework for building dynamic websites. Based on the MVC development pattern, Codeigniter can be modified to use the HMVC pattern. This pattern allows developers to maintain controller, view, and models in a sub-directory format. This framework is known for its higher speed as compared to other PHP frameworks. If you are looking for Codeigniter interview questions, we can help you!

A Quick Overview of CodeIgniter
What is Codeigniter? It is a PHP open-source web development framework for building dynamic websites. Based on the MVC development pattern, Codeigniter can be modified to use the HMVC pattern.
Latest Version 4.0, released on September 3, 2019
Stable Version 3.1.10, released on January 16, 2019
Created By EllisLab
Written in PHP
License MIT License

Most Frequently Asked CodeIgniter Interview Questions

Here in this article, we will be listing frequently asked CodeIgniter Interview Questions and Answers with the belief that they will be helpful for you to gain higher marks. Also, to let you know that this article has been written under the guidance of industry professionals and covered all the current competencies.

Q1. What is Codeigniter & why it is used for?
Answer

It is a PHP open-source web development framework which is used for building dynamic websites.

Q2. What is the latest version of Codelgniter? Lists its new features.
Answer

4.0 is the latest version of the Codelgniter framework. This version is released on September 3, 2019.

Here are some new features that support Codelgniter

  • URL Helper
  • Query builder
  • Email Library
  • Form Validation Library
  • Session Library

Codelgniter updated these modules to enhance features.

Q3. List the databases supported by Codeigniter?
Answer

Currently Codeigniter supports these databases that's are given below :-

  • MySQL
  • Oracle
  • PostgreSQL
  • SQLite
  • ODBC
  • Firebird etc
Q4. What is hooks and how we can configure hooks in CodeIgniter?
Answer

It is a type of events which can be called before and after the execution of a program in CodeIgniter. For example Hooks can be used where we need to check whether a user is logged in or logged out before the execution of controller.

It can be globally enabled or disabled by setting in the application/config/config.php page

It can be defined in application/config/hooks.php page. In CodeIgniter each hook is specified like an array with this prototype:

Q5. How to add or load a model in CodeIgniter?
Answer

You can do it with this $this->load->model("ModelName");

Q6. How to load a view in CodeIgniter?
Answer

You can use this $this->load->view('name'); to load a view in CodeIgniter.

Q7. What is the default controller used in CodeIgniter? How we can change it?
Answer

In CodeIgniter when a specified file in the default controller loaded by default when there is no file name mentioned in URL. By default "welcome.php" file is loaded after installing CodeIgniter.

Yes, we can change it from application/config/routes.php. We have to pass our controller name which we want to load by default in $route['default_controller']

For example, now the frontend is the default controller $route['default_controller'] = 'Frontend'; from now. Interview questions on Codeigniter are always a level up and thus a little tough to crack.

Q8. Explain the basic Url Structure in Codeigniter?
Answer

The basic URL structure is bestinterviewquestion.com/class/function/ID.

Here class represents controller's class that needs to be invoked.
Here "function" is name of method which is called.
ID is an additional parameter that is passed to controllers.

Q9. What do you mean by helper in CodeIgniter?
Answer

Helpers is collection of functions which help us with different tasks as name suggested. Every helper function is used to performs specific task with no dependence on other functions.

CodeIgniter provide various types of helper class like url_helper, captcha_helper ,email_helper. All these helpers are located in system/helper.

By default CodeIgniter does not load any Helper files. First step is to be load Helper. After loaded it is available in all controller and views.

 

How we can load a Helper : $this->load->helper('name');
Here "name" is file name of helper.

How to load multiple Helpers at one time.
$this->load->helper(array('helper1', 'helper2', 'helper3')); // Here helper1,helper2,helper3 are different helper's name.

Q10. Explain the difference between helper and library in CodeIgniter?
Answer

Helper is a set of Common functions which we can use within Models, Views, Controllers everywhere. Once we include that file then we can get access to the functions.
Library is a class which we need to make an instance of the class by $this->load->library() this function.

NOTE : A library is used in object-oriented context but a helper is more suitable to be used within the Views

Q11. What do you mean by Get_instance in CodeIgniter?
Answer

It is a globally available methods or functions that returns the Controller super-object which contains all the currently loaded classes. get_instance() function  returns the Controller class instance.

Q12. What do you mean by routing in Codeigniter?
Answer

It is the process of taking a URI endpoint and decomposing it into parameters to determine module controller and action of that controller should receive the request.

 

CodeIgniter has a userfriendly URI routing rule so that we can re-route URL easily . There is a one-to-one relationship between a URL string and its corresponding controller class and its methods.

 

Routing is a technique through which we can converts SEO friendly URLs into a server code format that easily understandable.

 

We can manage these from routes.php file at the located in application/config.

Q13. List some different hook point in Codeigniter?
Answer

Codeigniter supports various hook points.

  • post_controller_constructor
  • pre_controller
  • post_sytem
  • pre_system
  • cache_override
  • display_override
  • post_controller

If you are scrolling on the web for Codeigniter interview questions for 2 year experience pdf, then this guide will gonna help you.

Q14. How to link images/CSS/JavaScript from a view in codelgniter?
Answer

We can use an absolute path to resources to link images/CSS/JavaScript from a view in CodeIgniter.

For Example :

<link rel = "stylesheet" type = "text/css" href = "<?php echo base_url(); ?>css/responsive.css">
<script type = 'text/javascript' src = "<?php echo base_url(); ?>js/jquery.js"></script>

Q15. What do you mean by inhibitor in CodeIgniter?
Answer

Inhibitor is an error handling class. It is using the native PHP functions like set_error_handler, set_exception_handler, register_shutdown_function to handle all error's like parse errors, exceptions, and fatal errors.

Q16. How to remove index.php from URL in Codeigniter?
Answer

You can follow these given steps to remove index.php from URL in Codeigniter.

  • Please open config.php and change from $config['index_page'] = "index.php" to $config['index_page'] = "";
  • Update also from $config['uri_protocol'] ="AUTO"; to $config['uri_protocol'] = "REQUEST_URI";
  • Put this code in your htaccess file
    RewriteEngine on
    RewriteCond $1 !^(index\.php|resources|robots\.txt)
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [L,QSA]
Q17. What is CSRF and how you can enable CSRF in CodeIgniter?
Answer

In codeIgniter CSRF token is a random generated value that changes with each HTTP request sent by webform. When the form is submitted by user then website checks this submitted CSRF token equals or not the saved in the session.

We can enable CSRF protection from config.php and update the following values

$config['csrf_protection'] = TRUE;
$config['csrf_token_name'] = 'csrf_test_name'; //The token name
$config['csrf_cookie_name'] = 'csrf_cookie_name'; //The cookie name
$config['csrf_expire'] = 7200; // It will expire after this given time.

Q18. How to extend the class in Codeigniter?
Answer

You have to build a file with name application/core/Blog.php and declare your class with

Class Blog extends CI_Input {
      // Write your code here
}

Q19. What is Codeigniter Library and how we can load it?
Answer

Codeigniter provides a rich set of libraries, which indirectly increase the speed of developing an application. These Codeigniter system library is located in system/libraries.

In Codeigniter the library can be loaded with $this->load->library('class name');

If we want to load multiple libraries then we can use like $this->load->library(array('comment', 'blog'));

Q20. How to handle sessions in Codeigniter?
Answer
How to add values in session in Codeigniter

$_SESSION['username'] = 'bestinterviewquestion.com' // It can be use in core PHP
$this->session->set_userdata('username', 'bestinterviewquestion.com'); // It can be done in Codeigniter

We can pass array to store values in session in Codeigniter.

$array = array(
    'username' => 'bestinterviewquestion.com',
    'email' => '[email protected]',
    'url' => 'https://www.bestinterviewquestion.com'
);

$this->session->set_userdata($array);

How to remove values in session in Codeigniter

unset($_SESSION['username']); // It can be use in core PHP
$this->session->unset_userdata('username'); // It can be done in Codeigniter

If we want to remove more values from session then we can use like this

$this->session->unset_userdata($array);  // $array is defined above. It is an array with key & values.

How to get data from session in Codeigniter

$this->session->userdata('username'); // Here we will get username, if it is exists in session.

Q21. Explain why codeigniter is called as loosely based mvc framework?
Answer

It is called as loosely based mvc framework because in codeigniter controller is the necessary but model and view are optional. It means we can build a website without model.

Q22. How to install CodeIgniter? Explain step by step.
Answer

CodeIgniter can be installed with four steps that are given below:

  • Download it from its download page & then unzip the package.
  • Upload its files and folders on your server or localhost.
  • Open application/config/config.php file and set base URL.
  • If you want to use database then open application/config/database.php file and set your database credientails.
  • Now it's installed

If you want to configure additional setting then you can click here

Q23. What are the server requirements to install codeigniter?
Answer

We need only PHP version 5.6 or greater than 5.6. because of potential security and performance issues.

Q24. How we can load multiple helper files in Codelgniter?
Answer

You have to pass your helpers in array form. Here helper_1, helper_2, helper_3, helper_4 are different helpers that we want to load.

$this->load->helper(
    array('helper_1', 'helper_2', 'helper_3', 'helper_4')
);

Q25. How we can enable hooks in CodeIgniter?
Answer

If you want to enable hooks then you have to enabled/disabled by setting in the application/config/config.php file.

If you want to enable hooks then use this $config['enable_hooks'] = TRUE;

Q26. What do you mean by drivers and how we can create in CodeIgniter?
Answer

In CodeIgniter drivers are a very special type of Library that has a parent class and its child classes. Child classes have the access to its parent class but not their siblings. It provides an elegant syntax in your controllers file for libraries.

It can found in the system/libraries/ directory.


If we want to use a driver we have to initialize it within a controller with this given method.
$this->load->driver('class_name');


Here class_name is the name of the driver class which we want to invoke.

How we can create our own drivers in CodeIgniter.
  • You can create your own driver folder in /application/libraries/
  • Create your_Your_Driver_name.php file in /application/libraries/Your_Driver
  • In /application/libraries/Your_Driver/drivers
    • Your_Driver_name_subclass_1.php
    • Your_Driver_name_subclass_2.php
Q27. How we can print SQL statement in codeigniter model?
Answer

You can use $this->db->last_query(); to display the query string.
You can use print_r($query); to display the query result.

Q28. Explain how to create custom 404 page in CodeIgniter?
Answer

You can create your custom 404 page with these steps. Please follow these steps one by one.

 

  • You have to update your own created controller's class with $route['404_override'] = 'Controller_Class_Name';. You have to update this in your application/config/routes.php
  • Please create your new controller "Controller_Class_Name" file in your controllers directory application/controllers/.
  • Please put this code in your Controller_Class_Name.php

    class Controller_Class_Name extends CI_Controller {
         public function __construct() {
              parent::__construct();
         }

        public function index() {
             $this->output->set_status_header('404');
             $this->load->view('error_page_404');
        }
    }
  • Now create your "error_page_404" view file.
Q29. How to insert data into database in codeigniter?
Answer

You can use $this->db->insert(); to insert data in your database in Codeigniter.

If you want to insert this in your "Admin Table".

$dataArray = array(
       'name'=>'bestinterviewquestion.com',
       'phone'=>'9971083635',
       'email'=>'[email protected]'
);

$this->db->insert('Admin',$dataArray);

// Here "Admin" is My TableName

Q30. How to get last inserted id in codeigniter?
Answer

You can use $lastInsertID = $this->db->insert_id(); after insert query.

Q31. How to write join query in CodeIgniter?
Answer

$this->db->join('blog', 'comment.blog_id= blog.id', 'inner');

OR

$this->db->join('blog', 'comment.blog_id= blog.id');

Q32. Explain how to set and get config item value in Codeigniter?
Answer

To set config value $this->config->set_item('mainURL','bestinterviewquestion.com'); we can use this.

To get config value $this->config->set_item('mainURL'); we can use this.

Q33. How we can get Ip address in Codeigniter?
Answer

You can use $this->input->ip_address(); this to get IP Address.

Q34. How we can get current method or controller in Codeigniter?
Answer

You can use this $this->router->fetch_class(); to get current controller name.
You can use this $this->router->fetch_method(); to get current method.

Q35. How to use Pagination in Codelgniter?
Answer
Q36. How to use delete query in Codeigniter?
Answer

You can use $this->db->delete('admin', array('id' => 5)); this query to delete record according to condition
// DELETE FROM admin WHERE id = 5

Q37. How to truncate table with query in Codelgniter?
Answer

// TRUNCATE table admin;
You can use this to truncate table
$this->db->from('admin'); // select admin table
$this->db->truncate(); // truncate admin table

OR

You can also use like this
$this->db->truncate('admin'); // truncate admin table

Q38. How to enable error log in CodeIgniter?
Answer

Step 1. Make /application/logs log folder writable
Step 2. Set $config['log_threshold'] = 1; in /application/config/config.php

Q39. How we can redirect a page in CodeIgniter?
Answer

We can use the redirect() function to redirect on any page.

$this->load->helper('url');       // Firstly you have to load url helper.
if (condition != TRUE) {
    redirect('login_page');
}

Q40. How we can connect multiple databases in CodeIgniter?
Answer

$database1 = $this->load->database('DB1', TRUE); // Connect with database 1
$database2 = $this->load->database('DB2', TRUE); // Connect with database 2