Loading…

CodeIgniter Interview Questions and Answers

By Admin
Last update: 21 Jun 2020, 40 Questions
CodeIgniter Interview Questions

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
Official Website https://codeigniter.com

Most Frequently Asked CodeIgniter Interview Questions

1. What is Codeigniter & why it is used for?

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

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

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.

3. List the databases supported by Codeigniter?

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

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

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:

5. How to add or load a model in CodeIgniter?

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

6. How to load a view in CodeIgniter?

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

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

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.

8. Explain the basic Url Structure in Codeigniter?

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.

9. What do you mean by helper in CodeIgniter?

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.

10. Explain the difference between helper and library in CodeIgniter?

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

11. What do you mean by Get_instance in CodeIgniter?

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.

12. What do you mean by routing in Codeigniter?

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.

13. List some different hook point in Codeigniter?

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.

15. What do you mean by inhibitor in CodeIgniter?

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.

16. How to remove index.php from URL in Codeigniter?

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]
17. What is CSRF and how you can enable CSRF in CodeIgniter?

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.

18. How to extend the class in Codeigniter?

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
}

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

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'));

20. How to handle sessions in Codeigniter?
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.

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

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.

22. How to install CodeIgniter? Explain step by step.

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

23. What are the server requirements to install codeigniter?

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

24. How we can load multiple helper files in Codelgniter?

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')
);

25. How we can enable hooks in CodeIgniter?

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;

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

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
27. How we can print SQL statement in codeigniter model?

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

28. Explain how to create custom 404 page in CodeIgniter?

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.
29. How to insert data into database in codeigniter?

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

Example

                                                    

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

30. How to get last inserted id in codeigniter?

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

31. How to write join query in CodeIgniter?

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

OR

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

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

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.

33. How we can get Ip address in Codeigniter?

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

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

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.

36. How to use delete query in Codeigniter?

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

37. How to truncate table with query in Codelgniter?

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

38. How to enable error log in CodeIgniter?

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

39. How we can redirect a page in CodeIgniter?

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

Example

                                                    

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

40. How we can connect multiple databases in CodeIgniter?

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

History of Codeigniter

The first version was released in Feb 2006 by EllisLab.

Advantages of Codeigniter

If you are preparing for Codeigniter interview questions, do memorize the powers of CodeIgniter.

  • Easy to learn, handle and customize
  • Flexible and easy to configure
  • Amazing Active Record Implementation
  • Best-in-class documentation
  • Discount, Offers, Coupons and Options and Properties
  • Extensive user base