CodeIgniter Interview Question

CodeIgniter Interview Question

by Pooja Chikara 15-Apr-21

0 485

Everybody gets nervous preparing for an interview and it's normal to revise everything and being prepared beforehand. To take care of your nervousness and get you prepared for an interview, we have come up with the most asked and important questions that you will definitely encounter in any interview regarding CodeIgniter.

Proving your worth in front of an Interviewer is of utmost importance in today's challenging and cut-throat competition world. So, get prepared and learn from the best.

These questions are curated by our experts to take care of both freshers and experienced candidates.

List of CodeIgniter Interview questions and answers

1 Explain what is Codeigniter?

CodeIgniter is an MVC (Model View Controller) framework that is used to generate websites by using PHP programming language. It is lightweight, secure, fast, and free (open source) to use. It generates clean and browser-friendly URLs.


2 What is the current version of Codeigniter?

On February 28, 2006, EllisLab released the first public version of CodeIgniter. The current version of CodeIgniter is v3.1.11 which works with PHP 5.6 and earlier versions. If you want to download Click here!


3 How to check the version of the CodeIgniter framework?

To check the version of your Codeigniter framework follow these steps:

  • Open system/core/Codeigniter.php file.
  • Search variable CI_VERSION in it.
  • The value of this variable is the version of your Codeigniter framework.


4 List Databases supported By Codeigniter Frameworks?

The databases supported by Codeigniter are listed below:

  • MySQL (5.1+)
  • Oracle
  • PostgreSQL
  • MS SQL via the MsSQL, Sqlsrv (version 2005 and above only
  • SQLite
  • CUBRID
  • Interbase/Firebird
  • ODBC

5 List some features provided by CodeIgniter.

Some attractive features of CodeIgniter are given below:

  • Dynamic installation
  • MVC-based architecture
  • Loose coupling
  • Extremely Light Weight
  • Provides the facility of error logging and full-page caching
  • Generates browser-friendly URLs.
  • Provides form and data validation.
  • Provides data encryption, session management, and pagination facilities
  • Supports hooks and class extensions.

6 Explain helpers in CodeIgniter and how to load helper file?

  • In Codeigniter, a helper is a collection of independent procedural functions in a particular category.
  • Each helper function performs one specific task, with no dependence on other functions.
  • Helpers are stored in the application/helpers directory.

To loading a helper into a certain controller use the following lines:

$this->load->helper('helper_name');

Where helper_name is the name of the helper which you want to load.

To read more about helpers Click here!


7 What are hooks in CodeIgniter?

  • In Codeigniter, hooks provide the facility to change the inner working of the framework without changing the core files.
  • Hooks are used to performing any action at a particular stage in the execution process.
  • Hook files are stored in the application/hooks directory of your project.
  • All hooks-related configurations are done into the applicaton/config/hooks.php file.
  • Some interesting points about hooks are listed below:

  • pre_system: This function is called at the starting phase of system execution. Only the benchmark and hooks class have been loaded at this point.
  • pre_controller: This function is called immediately prior to any of your controllers being called.
  • post_controller_constructor: This function is called immediately after your controller is executed.
  • post_controller: This function is called immediately after your controller is fully executed.
  • display_override: This function is used to send the finalized page to the web browser at the end of the system. It overrides the display() method.
  • cache_override: This permits you to use your own cache display mechanism instead of the _display_cache() method.
  • post_system: This is called at the end of system execution after the finalized data is sent to the browser.

To read more about hooks Click here!


8 List Common Functions in Codeigniter.

Codeigniter provides some functions that are used globally defined. Some common functions are listed below:

  • is_php()
  • is_really_writable()
  • config_item()
  • set_status_header()
  • remove_invisible_characters()
  • html_escape()
  • get_mimes()
  • is_https()
  • is_cli()
  • function_usable()

To read more Click here!


9 How do you set default timezone in Codeigniter?

To set default timezone in Codeigniter follow these steps:

  • Open application/config/config.php file.
  • Put the given code into it.
date_default_timezone_set('your timezone');

10 How to add/link images/CSS/JavaScript from a view in CI?

We can add an image or a link by using the base_url() function.

To use the base_url() function, you have to load the URL helper into your controller file.

$this->load->helper('url');

Now, see the given example for your reference

To link CSS file use,

<link rel = "stylesheet" type = "text/css" href = "<?php echo base_url(); ?>css/style.css">

To link JS file use,

<script type = 'text/javascript' src = "<?php echo base_url(); ?>js/script.js"></script>

To add an image use,

<img src="<?php echo site_url('images/myimage.jpg'); ?>" />

For more details Click here!


11 How can you remove index.php from the URL in Codeigniter?

Here are few simple steps by following them we can easily remove index.php from our application URLs and make them friendly for users and search engines.

  • Open the application/config/config.php file and search index_page in it and make this parameter value blank.
  • $config['index_page'] = '';
  • Create a .htaccess file at the root directory of your application and save the given code in that file.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

12 How will you add or load a model in Codeigniter?

In Codeigniter, a model is used to handles the data stored in the database.

We can load a model with the help of a controller.

$this->load->model("model_name");

Explanation:

  • this keyword is used for self-referencing.
  • load is the object of CI_Model class which we inherit in our First_model class.
  • model is a method of CI_Model class which we used to load the content of the given model.

To use the method of the loaded model, we have to use the following code.

$this->model_name->method_name();

Where model_name is the name of model and method_name represents a method of model_name model.

To read more about Codeigniter models Click here!


13 What are Sessions In CodeIgniter? How to read, write or remove session in CodeIgniter?

Sometimes we need to track user’s activity and state when they are browsing our application. We can do this with the help of session management.

We know that sessions are work in a global environment means we can access session data on every page of the application. To use the sessions, first of all, we have to initialize them.

$this->load->library('session');

After adding the above line into the controller, we can use the session object to create sessions as:

$this->session

Adding session data:

$this->session->set_userdata('name', 'value');

Where name is the name of a variable and value is the value that assigns to the name variable.

Retrieving session data:

$name = $this->session->userdata('name');

Remove Session Data:

$this->session->unset_userdata('some_name');

Destroying a Session:

$this->session->sess_destroy();

To read more about sessions click here!


14 How to get the last inserted id in CodeIgniter?

In Codeigniter, you can use the insert_id() method of the DB class to get the last inserted id.

Example:

function add_db($data){
$this->db->insert('user', $data);
$insert_id = $this->db->insert_id();
return $insert_id;
}

15 How you will use or load CodeIgniter libraries?

The CodeIgniter libraries are powering efficiency, code reusability, separation, and simplicity.

In CodeIgniter, we have two types of libraries:

  • Built-in libraries: These are stored in the system/libraries directory.
  • Custom libraries: These are created in the application/libraries directory.

Loading a library:

$this->load->library('my_lib');

Here, my_lib is the name of the library that we want to load.

To load more than one library, we use multiple arrays that contain all libraries.

$this->load->library(array(' form_validation ', ' pagination '));

To read more about Codeigniter libraries click here!


16 What is_cli() method does in Codeigniter?

Is_cli() function is used to determine the application is run through the command line or not.

The return type of this function is bool and this function returns TRUE if currently running under CLI, FALSE otherwise.


17 Explain what is an inhibitor in CodeIgniter?

In codeigniter, inhibitor is an error handler class which used to handle parse errors, exceptions, and fatal errors by using PHP native functions like set_exception_handler(), set_error_handler(), register_shutdown_function().


18 Explain how you can prevent CodeIgniter from CSRF?

CSRF stands for Cross-Site Request Forgery. We can enable CSRF protection by changing our application/config/config.php file in the following way:

$config['csrf_regenerate'] = TRUE;

If you are using form helper in your application and creating all your forms by using the form_open() function, then this setting from the config.php file will automatically insert a hidden CSRF field in your forms.

We can also add CSRF field manually by using get_csrf_token_name() and get_csrf_hash()

$csrf = array(
'name' => $this->security->get_csrf_token_name(),
'hash' => $this->security->get_csrf_hash()
);

...

<input type="hidden" name="<?=$csrf['name'];?>" value="<?=$csrf['hash'];?>" />

To read more about securities in Codeigniter click here!


19 Mention what are the security parameters for XSS in CodeIgniter?

XSS stands for Cross-Site Scripting. To trigger JavaScript or other types of code that attempt to hijack cookies or do other malicious things, Codeigniter provides a Cross-Site Scripting prevention filter.

We can use xss_clean() function to filter data through the XSS filter.

$data = $this->security->xss_clean($data);

Where,

  • this: This keyword is used for self-referencing.
  • security: It is the object of CI_Controller class that we inherit in the controller class.

This function most commonly used to prevent cross-site scripting during the form submission.

It has another optional Boolean parameter which used to check the image file for the XSS attack.

if ($this->security->xss_clean(, TRUE) === FALSE)
{
// file failed the XSS test
}

20 How can the user connect models to a database manually?

The database configuration enables to define one or more database connections that can be used by the application.

The database configuration is done by editing the application/config/database.php file.

Manual loading is done in the controller which you are using for application working. Open your controller file and writes the following line.

$this->load->database();

If you want to load a specific database, then pass the name as a parameter in the database() method.

$this->load->database('group_name');

Where group_name is the name of a group of the database that you want to connect.


21 Elaborate MVC in CodeIgniter.

Codeigniter is based on MVC architecture. MVC stands for "model-view-controller". In the MVC pattern, the application logic is isolated from the presentation. It is a software approach that provides the facility to split the entire source code into three major parts: model, view, and controller.

  • Model: It represents data structures that help to retrieve, insert and update the information into the database. These are operated by the controller. A model in Codeigniter is a PHP class that is designed to work with the information in the database.
  • View: It represents the information presented to the user. A view can be a page or a small part of any page. It can be a webpage or an RSS (Really Simple Syndication) page. The view is the HTML content executed by the user browser that presents and interacts with the user.
  • Controller: A controller is a basic unit in MVC architecture that full fill the request of the user and presents the relevant data to the user. It works as an intermediary unit among the model, view, and any other processes needed to process the HTTP (HyperText Transfer Protocol) request.

22 Elaborate the views in the CodeIgniter.

A view is a webpage or part of a webpage that contains HTML content executed by the user browser. Views interact directly with the user. View files are created into the application/views directory.

Syntax:

<html>
<head>
<title>page Title</title>
</head>
<body>
.......
</body>
</html>

23 How can you load a view in CodeIgniter?

We can access the view file with the help of controllers.

$this->load->view("view_name");

Explanation:

  • this: This keyword is used for self-referencing.
  • load: The load is the object of CI_Controller class which we inherit in the controller class.
  • view: This is a method of CI_Controller class that we used to display the content of a view file.
  • view_name: It represents the name of the view which we want to access.

24 What do you mean by the controller in CodeIgniter?

In Codeigniter, a controller works as an intermediary unit among the model, view, and any other processes needed to process the HTTP (Hypertext transfer protocol) request.

Syntax:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Controller_name extends CI_Controller{

public function index()
{
}
}
?>

Explanation:

The very first line is used to make the controller secure means one cannot access directly the controller files by entering the filename in the URL.

Controller_name: It represents the name of the controller which we want to create. We have some rules to define the name of a controller.

1 The first character of the name should be in uppercase.

2 The name of the file and the name of the controller should be the same.

CI_Controller is a predefined Codeigniter class. Every controller extends this class to inherit the predefined public functions and variables of CI_Controller.

Index(): It is the default function of the controller and it's mandatory.

25 What is the default controller in CodeIgniter?

By default, the file name is welcome.php which is known as the first page that is to be seen after installing CodeIgniter. So, the welcome is the default controller in Codeigniter.

We can change the default controller by changing the default_controller variable in the application/config/routes.php file.

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

Where controller_name is the name of the controller that you want to use as the default controller.


26 What is the basic CodeIgniter URL structure?

The basic Codeigniter URL structure is given below:

Project_name/index.php/controller_name/function_name
  • Project_name: it is the name of the project.
  • controller_name: It is the name of the controller which we want to call. If we don’t give this parameter into the URL then by default Welcome controller is called.
  • function_name: It is the name of the method which we want to execute inside the controller. If we do not give this parameter into the URL then the index() method is executed by the controller.

27 How can the programmer create a library in CodeIgniter?

These libraries are stored in the application/libraries folder. There are three methods to create a custom library.

  • Creating a new library
  • Extending a native library
  • Replacing a native library

To read more click here!


28 Can a programmer extend the native libraries in CodeIgniter?

Codeigniter provides the facility that you can add one or more methods in the old library to extend the functionality of it. There are some rules which should be followed:

  • The New class extends the native library class.
  • Every new class start with the prefix MY_.

For example:

To extend the CodeIgniter table library. Open application/ libraries directory. Create a file MY_Table.php and create a class in it which extends the CI_Table class as:

Class MY_ Table extends CI_ Table {
…………
}

29 How can a constructor be called in CodeIgniter?

In Codeigniter, a constructor can be called by using the following line.

parent::__construct();

30 Explain how you can extend the class in Codeigniter?

In CodeIgniter, you have to build a file named application/core/M_Input.php and declare your class to extend the native input class.

Class M_Input extends CI_Input {

}

These are the most important questions that one should be familiar with before facing an interviewer. Hope you are prepared now and All the best




Share:


Leave a Reply


Comments
    Waiting for your comments