Top Interview Questions and Answers

Top Interview Questions and Answers
Interview Sortout

Browsing "Older Posts"

Browsing Category "Codeigniter"

What is New In CodeIgniter 4.x?

By Dhananjay → December 4, 2018

Hooks in CodeIgniter

By Dhananjay → September 11, 2018

Hooks in CodeIgniter

In code igniter hooks are the points before and after the execution where we can call some specific functions. These functions are called hooks and points where we call hooks are called hook point. Hooks are usefull when we have to make some changes throught the application , then we can do this without hacking all the files, just adding one hook at appropriate point. We can modify the way of usual rendering in code igniter application. So hooks facilitates modification in functionality without changing the core.

How we can use a hook in code igniter framework?
Before we use any hook in application we need to enable the hook in config.php file.Set the enable_hook config variable : TRUE,

$config['enable_hooks'] = TRUE;

The framework provide number of Hook Points to ease the development and modification in application:

The pre_system is called very early during system execution. Only the benchmark and hooks class have been loaded at this point.

As per the name just called before any of your controllers being called. When all the base classes are loaded, routing and security checks are done a pre_controller hook is called.

The hook is called just after the controller class instantiated but none of the method is called.

Called immediately after your controller is fully executed.

We have the _display() function in our core and we use it to render our view on browser after execution of all programms.
display_override allow use to display our own content at desired places.

$this->CI => get_instance();

We can call our own function instead of _display_cache() function using the cache_override hook. We can define our own cache display mechanism.

At the end of the system execution when final rendering is done post_system is called.

Define a hook :
A hook is defined in the hooks.php file located at application/config

Example for define a pre_controller hook:

$hook['pre_controller'] = array(  
            'class' => 'Classname',  
            'function' => 'functionname',  
            'filename' => 'filename.php',  
            'filepath' => 'hooks',  
            'params' => array('element1', 'element2', 'element3')  

Top 10 Codeigniter Interview Questions and Answers for Experienced

By Dhananjay → May 31, 2018

1- What is hook?
CodeIgniter’s Hooks feature modify the core functionalitis of the framework without changing the core files. For example, If you want to run a script right before your controllers get loaded, or right after or at any other location, you can use Hook.

2- How many types of hooks are there in Code Igniter?
There are 7 availabe hook points:

pre_system :
Called very early during system execution. Only the benchmark and hooks class have been loaded at this point. No routing or other processes have happened.

pre_controller :
Called prior to controllers being called. All base classes, routing, and security checks have been done.

Called after controller is instantiated, but prior to any method calls happening.

Called after the controller is executed.

Overrides the _display() method, used to send the finalized page to the web browser at the end of system execution. This permits you to use your own display methodology. Note that you will need to reference the CI superobject with $this->CI->get_instance() and then the finalized data will be available by calling $this->CI->output->get_output().


Enables you to call your own method instead of the _display_cache() method in the Output Library. This permits you to use your own cache display mechanism.

Called after the final rendered page is sent to the browser, at the end of system execution after the finalized data is sent to the browser.

Creating a hook :
'class' => 'YourClassname',
'function' => 'your_function_name',
'filename' => 'your_filename.php',
'params' => array('p1', 'p2')
'filepath' => 'hooks', );
$hook['pre_controller'] = $my_hook;

3- Can we use multiple call to same hook?
Yes , we can use multiple call to same hook. Then We have to use multidimentional array:
$hook['pre_controller'][]= $hook_one;
$hook['pre_controller'][]= $hook_two;

4- What are the benifites of using hook in an application?
-We can modify core functionalitis without touching the core files.
-We can generate derired output without changing the view files.
-we can make a hook for recurring calls , like including footer and header in each function, without calling them in each controller function.
-we can create a script to minify the code before excecution take place.
-We can easily implement security checks, privacy, session, cache using hooks and can seperate these implementation from the main functionalitis.

5- WHat is HMVC?
HMVC is a Modular version of MVC. In this system we can implement modules for particular purpose. We may have put out controller , views and model in different modules. All these modules will be available in modules folder in application. HMVC stands for Hierarchical Model View Controller.

6- What is a module?

A module is a part of application and a full featured block which is implemented on HMVC pattern. A module contains its own Model, View and Controller. Although these resources can be shared with other modules.

7- What is difference between Library and Helper?

A CodeIgniter helper is a set of related global functions, you can call them any where in application. Once you load the helper file you can directly call the function. Basically the helper is procedural.

But a Library is a class, which you need to make an instance of the class. Once the library is loaded, we need to use $this operator to call the Library function. This is Object Oriented.

8- What is Routes in Code Igniter Framework?

Routing is the communication between URI and Controller. Routing decides the controller calls and parameters in code igniter. We can overwrite the routing rules in routes.php file.

9- How to use or enable libraries CodeIgniter?
We can initialize the library in controllers as given below:

10- Why CodeIgniter is a loosely coupled mvc framework?
CodeIgniter is called a loosely coupled mvc because, it provides unlimited flexibility to developers. It allows us to write and execute our custom codes, library, helpers. We can use call model and controllers functions in views and also can write custom code on views. It works like a core php when some one is going to manipulate it and on the other side it provide a modular structure to our application, XSS and CSRF functionalitis. It provides us lots of inbuilt library and also compatible with many more custome libraries. It never restricts us to write and execute any code.

15 Advance Code Igniter Interview Questions and Answer

By Dhananjay → January 23, 2018

How routing is beneficial in Code Igniter?
Routing provides a way to alter your URL and redefine it according to you. It helps in security and SEO. We can create URL having keywords and phrases influential for SEO ranking.

What is a helper?
Helpers are the file having functions for specific purposes. We can directly call them in our script.There is numerous type of helper:
  • Array Helper: It has functions that we use to manipulate arrays.
  • File Helper: Use of this helper is to play with files as upload an image.
  • HTML Helper : Html helper is used to compose HTML dom using php code.
  • URL Helper: We use it to create and construct or redirect URLs.

What does autoload.php do?
It loads when the application start loading contains and default parts of application call. It has information to load the segments that we need to load automatically. For example, we may need to load some models automatically.

How can we connect code igniter to our database?
There is a database.php file in the config directory. We can put here our database information like database name, hostname, username and password and It will be connected.

Can we call multiple model function in a single controller function?
Yes, of course. We can call any number of model and their functions in a single controller class?

Can we write database queries in a controller?
Yes, we can write database queries directly in controllers but this is not a good practice. We should write database queries always in the model.

How can we pass data to view?
We create an array having a particular key and pass this array to load view call. We will have this data on the view page as having the name of the key given to data array.

Now on the employee_info.php page, we will have a $employee variable and that will store the data passed in the array $data in the controller.

How can we pass a parameter to controller function?
A parameter can be passed to URL segment after backslash next to function route. Suppose "admin" is a controller , "all_user" is a function and "active" is a parameter. Now we have to pass this to function that it can list the active user so we can call as given below:

<?php echo site_url("admin/all_user/active")?>

What is a library in Code Igniter?
Code Igniter library is a utility to extend the functionality and to use some predefined functions and classes. We have different libraries for different purposes as upload, database etc.

How can we execute a raw query in Code Igniter?
Using query builder library of code igniter we can execute raw query. For example,


What is URL helper?
We use URL helper to do manipulation with URL. We can easily create custom URL, we can extract parameters from URL, we can easily create custom anchors, form action, and page redirection.

How the database library provide strength to Code Igniter framework?
Database library has query builder functions. It works with single data and also in batch data. It accepts the array and single values as parameters. Functions are very similar to MySQL query phrases and keywords. We can use almost the query using query builder. It fetches the data in all possible formats. We can select, update, join, delete the records in very simple and robust way.

How we will submit data using a form on code igniter view?
We can give the controller function route as the form action. When we will submit this for, the controller function will be called and data will be collected using $_GET or $_POST arrays, as the method defined in the form.

How we create a session variable and how will we get that value stored in a session variable?
Code igniter has session library.It uses four types of the driver to store session data:
The file is the default storage.
After loading session library we can use session class and its method to set or get the session value.

//set session data
$this->session->set_userdata('user_session_name', 'session_value');

//check session
$this->session->has_userdata('user_session_name', 'session_value');

//get session data
$this->session->get_userdata('user_session_name', 'session_value');

//destroy session data

How can we upload any file in Code Igniter application?
Code igniter has upload library. We will use this library and set preferences in $config variable.
After initializing library we use call do_upload() function and it will complete the task.

Code igniter Interview Questions and Answers

By Dhananjay → January 19, 2018

What is MVC?
MVC is a three-tier architecture of application development approach.Almost the popular languages use this pattern. MVC provides Modular structure to an application. Three interconnected parts are Model, View, and Controller.
M: Model, that means data handling and information interchange. Implement CRUD.
V: view i.e. front end and representational parts, where user interacts with machine
C: Controller, The business login, handle user request an passes to model and in return provide results to the user.

What is difference b/w MVC and HMVC?
HMVC is a hierarchical MVC, The whole application is modularized in interconnected modules and each module has its own MODEL, VIEW, CONTROLLER. In HMVC a module is a complete package of User Interface, business logic, and data handling. So MVC is discrete whereas HMVC contains MVC at each level.

How we can load a model in CI controller?
We can call load a model in the constructor as given in the following example:

//load model $this->load->('users_model','users');
//call model

We can also assign an alias to ci_model. We can use 'users' to call a model rather than calling 'users_model'.

What is autoload.php in the config?
This is a file where preloaders are defined. Suppose we want to include database library, any model, URL library, sessions etc, then we can assign all these things here.

What helpers do in CI?
Helpers are the simple PHP functions returning some values and we can call them anywhere in our project once we initialized them.

What are the libraries in CI, explain some popular libraries?
The library is the packages which are designed to implement a particular set of functionalities. For example Database. Once we Initialized it, we do any operation on the database.

What are Hooks?
Hooks provide flexibility to CI core. We can modify the core functionality of CI without disturbing core files and use hooks.Learn more about Hooks in CodeIgniter

How we define URL and redirect in CI?
We use URL helper and then we can create redirect links :


these functions provide the way to create URL.

How to write a SELECT query in CI model?
We can write a select query in code igniter using the standard conventions of CI as given below:

//table name 'users' $this->db->select('users');

How to call a CI model Controller?
The following example illustrates, how to call a model in code igniter controller:

//load model $this->load->('users_model','users');
//call model

What are the views?
A view is the display part of the application. It renders the results on screen and provides UI.We can assemble views files in directories as well.

How we define controller in CI?
A controller is the backbone of MVC. It creates a connection between model and view.We can define a controller as illustrated:

'BASEPATH') OR exit('No direct script access allowed');

How controllers and models are different?
Controllers deals with business logic. When user Instruct the machine to do something, controller listens to that response while interaction another side with a model for data. Whereas a model is data handling layer. It saves data to the database and it requests data from the database that further goes to the controller to display on view.

What is the convention to call controller function with a parameter in CI?
We can call a controller function in uri using the a pattern given below. First part of URI is base URL. Base URL followed by controller route and followed by function name , parameters.
[Controller Class]/[controller methos]/[parameters]

How to write JOIN Query in Code Igniter?
Code Igniter has a very rich Database library. It provides a flexible way to call and create logics. Either we can use raw queries or we can use join() function.

    $this->db->from('table1 as t1');
    $this->db->join('table2 as t2', ' = t2.user_id', 'left');
    $query = $this->db->get();
    return $query->result();

How to apply where clause in Code Igniter?
Using Code Igniter Database library function 'where()' we can apply where clause.

$where=array( 'email'=>,

What is routing?
Routing is a way to define URI and how to call controller methods with a particular URI.
Routing provides flexibility to change the way we call controller functions. We can rename controller and functions. This enhances the security features as well as we can create SEO friendly URLs.

What is the way to fetch query results in CI?
Ther are many ways to fetch result in Code Igniter. Some of them are as follows:

How we call controller method using AJAX?
Simply we can call the URL using proper method and parameters, and then handle those request on a controller and if required we can render a view.

How to define a session variable in CI?
First of all, we need to load session library and then we can call some library functions to accomplish this task:

//load library $this->load->library('session');
//define a session variable named 'user_id'
$this->session->set_userdata('user_id', $user_id);