Codeigniter Interview Questions and Answers

codeigniter-interview-question-answer

1) What is CodeIgniter?

CodeIgniter is an open source and powerful framework used for developing web applications on PHP. It is loosely based on MVC pattern and similar to Cake PHP. It is very easy to use compare to other PHP frameworks.


2) What are the most prominent features of CodeIgniter?

A list of most prominent features of CodeIgniter:

  • It is an open source framework and free to use.
  • It is extremely light weighted.
  • It is based on Model View Controller (MVC) pattern.
  • It has full featured database classes and support for several platforms.
  • It is extensible. You can easily extend system by using your own libraries, helpers etc.
  • Excellent documentation.

3) Explain CodeIgniter architecture.

From technical point of view, CodeIgniter is dynamically instantiation (light-weighted), loosely coupled (components rely very less on each other) and has component singularity (each class and functions are narrowly focused towards their purpose).


4) Explain what are hooks in CodeIgniter?

Codeigniter’s hooks feature provides a way to change the inner working of the framework without hacking the core files. In other word, hooks allow you to execute a script with a particular path within the Codeigniter.  Usually, it is defined in application/config/hooks.php file.


5) Explain how you will load or add a model in CodeIgniter?

Within your controller functions, models will typically be loaded; you will use the function

  • $this->load->model (‘Model_Name’);

6) Explain model in CodeIgniter.

Model’s responsibility is to handle all data logic and representation and load data in the views. It is stored in application/models folder.


7) How can you connect models to a database manually?

To connect database manually use following syntax,


8) Explain views in CodeIgniter.

View folder contains all the markup files like header, footer, sidebar, etc. They can be reused by embedding them anywhere in controller file. They can’t called directly, they have to be loaded in the controller’s file.


9) How can you load a view in CodeIgniter?

View can’t be accessed directly. It is always loaded in the controller file. Following function is used to load a view page:

  • $this->load->view(‘page_name’);

10) Explain controller in CodeIgniter.

A controller is the intermediary between models and views to process HTTP request and generates a web page. It is the center of every request on your web application.


11) What is default controller in CodeIgniter.

The file specified in default controller will be loaded by default when no file name is mentioned in the URL. By default, it is welcome.php which is the first page to be seen after installing CodeIgniter.


12) How will you call a constructor in CodeIgniter?

To use a constructor you need to mention the following line of code,

  • parent::_construct()

13) Explain what helpers in CodeIgniter are and how you can load a helper file?

In CodeIgniter, helpers are group of function in a particular category that assist you to perform specific functions. In CodeIgniter, you will find many helpers like URL helpers- helping in creating links, Text helpers- perform various text formatting routines, Cookies- helpers set and read cookies.  You can load helper file by using command $this->load->helper (‘name’) ;


14) Explain routing in Codeigniter?

In CodeIgniter, the way PHP files served is different rather than accessing it directly from the browser. This process is called routing. Routing in CodeIgniter gives you freedom to customize the default URL pattern to use our own URL pattern according to the requirement. So, whenever there is a request made and matches our URL pattern it will automatically direct to the specified controller and  function.


15) What is basic CodeIgniter URL structure?

Instead of using ‘query-string’ approach, it uses a segment based approach.

Its structure is as follows,

class represents controller class that needs to be invoked.

function is the method that is called.

ID is any additional segment that is passed to controllers.


16) What is inhibitor in CodeIgniter?

In CodeIgniter, Inhibitor is an error handler class that uses native PHP functions like set_exception_handler, set_error_handler, register_shutdown_function to handle parse errors, exceptions, and fatal errors.


17) Why is there a need to configure the URL routes?

Changing the URL routes has some benefits like

  • From SEO point of view, to make URL SEO friendly and get more user visits
  • Hide some URL element such as a function name, controller name, etc. from the users for security reasons
  • Provide different functionality to particular parts of a system

18) List out different types of hook point in Codeigniter?

Different types of hook point in Codeigniter includes

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

19) What is the default method name in CodeIgniter?

By default controller always calls index method. If you want to call a different method, then write it in the controller?s file and specify its name while calling the function.


20) Explain how you can link images/CSS/JavaScript from a view in code igniter?

In HTML, there is no Codeigniter way, as such it is a PHP server side framework. Just use an absolute path to your resources to link images/CSS/JavaScript from a view in CodeIgniter

/css/styles.css

/js/query.php

/img/news/xyz.gpg


21) Explain remapping method calls in CodeIgniter.

Second segment of URI determines which method is being called. If you want to override it, you can use _remap() method. The _remap method always get called even if URI is different. It overrides the URI.


22) Explain what is inhibitor in CodeIgniter?

For CodeIgniter, inhibitor is an error handler class, using the native PHP functions like set_exception_handler, set_error_handler, register_shutdown_function to handle parse errors, exceptions, and fatal errors.


23) Mention what is the default URL pattern used in Codeigniter framework?

Codeigniter framework URL has four main components in default URL pattern.  First we have the server name and next we have the controller class name followed by controller function name and function parameters at the end. Codeigniter can be accessed using the URL helper. For example http://servername/controllerName/controllerFunction/parameter1/parameter2.


24) Explain how you can extend the class in Codeigniter?

To extend the native input class in CodeIgniter, you have to build a file named application/core/MY_Input.php and declare your class with

Class MY_Input extends CI_Input {

}


25) Explain how you can prevent CodeIgniter from CSRF?

There are several ways to protect CodeIgniter from CSRF, one way of doing is to use a hidden field in each form on the website.  This hidden field is referred as CSRF token; it is nothing but a random value that alters with each HTTP request sent. As soon as it is inserted in the website forms, it gets saved in the user’s session as well.  So, when the form is submitted by the users, the website checks whether it is the same as the one saved in the session. If it is same then, the request is legitimate.


26) Explain how you can enable CSRF (Cross Site Request Forgery) in CodeIgniter?

You can activate CSRF (Cross Site Request Forgery) protection in CodeIgniter by operating your application/config/config.php file and setting it to

$config [ ‘csrf_protection’] = TRUE;

If you avail the form helper, the form_open() function will insert a hidden csrf field in your forms automatically


27) How to access config variable in codeigniter?


28) How to unset session in codeigniter?


29) How do you get last insert id in codeigniter?


30) How to print SQL statement in codeigniter model??


31) What are hooks in CodeIgniter?
CodeIgniter’s Hooks feature provides a means to tap into and modify the inner workings of the framework without hacking the core files.


32) Explain Codeigniter File Structure.

When you download Codeigniter you will see the following folder structure :-

  • application
    • cache
    • Config
    • Controllers
    • core
    • errors
    • helpers
    • hooks
    • language
    • libraries
    • logs
    • models
    • thirdparty
    • views
  • system
    • core
    • database
    • fonts
    • helpers
    • language
    • libraries

33) What is the goal of CodeIgniter?

The goal is to enable you to develop projects with much faster than you could if you were writing code from scratch by providing a rich set of libraries for commonly needed tasks as well as a simple interface and logical structure to access these libraries.


34) Explain MVC in CodeIgniter.

Model–View–Controller (MVC) is an architecture that separates the representation of information from the user’s interaction with it.

Controller: The Controller serves as an intermediary between the Model, the View. controller mediates input, converting it to commands for the model or view.

Model: The Model represents your data structures. Typically your model classes will contain functions that help you retrieve, insert, and update information in your database.The model consists of application data and business rules.

View: The View is the information that is being presented to a user. A View will normally be a web page. A view can be any output representation of data.


35) Why is there a need to configure the URL routes?

Changing the URL routes has some benefits like:

  • From SEO point of view, to make URL SEO friendly and get more user visits.
  • Hide some URL element such as a function name, controller name, etc. from the users for security reasons.
  • Provide different functionality to particular parts of a system.

36) Mention what are the security parameter for XSS in CodeIgniter?

CodeIgniter has got a cross-site scripting hack prevention filter. This filter either runs automatically or you can run it as per item basis, to filter all POST and COOKIE data that come across. The XSS filter will target the commonly used methods to trigger JavaScript or other types of code that attempt to hijack cookies or other malicious activity. If it detects any suspicious thing or anything disallowed is encountered, it will convert the data to character entities.


37) How you will use or add CodeIgniter libraries?

All of the available libraries are located in your system/libraries folder. In most cases, to use one of these classes involves initializing it within a controller using the following initialization function:


38) How you will work with error handling in codeigniter?

CodeIgniter lets you build error reporting into your applications using the functions:-

1. show_error():- This function will display the error message supplied to it using template application/errors/error_general.php.

2. show_404() :- Function will display the 404 error message.

3. log_message(‘level’, ‘message’) :- This function lets you write messages to your log files. You must supply one of three “levels” in the first parameter, indicating what type of message it is (debug, error, info), with the message itself in the second parameter.


39) In Which language CodeIgniter is written?

PHP


40) What are the features of codeigniter? Open source framework

  • Light Weight
  • CodeIgniter is Extensible
  • Full Featured database classes

41) How to unset session in codeigniter?

We can use unsetuserdata to destroy particular session variable

this->session->unset_userdata(‘somename’);

We can use sessdestroy to destroy all session:

$this->session->sess_destroy();


42) How to get random records in mysql using codeigniter?

We can use this:

$this->db->order_by(‘id’,’RANDOM’);


44) Explain Application Flow Chart in codeigniter?

The following graphic illustrates how data flows throughout the system:
CodeIgniter application flow

The index.php serves as the front controller, initializing the base resources needed to run CodeIgniter.
The Router examines the HTTP request to determine what should be done with it.
If a cache file exists, it is sent directly to the browser, bypassing the normal system execution.
Security. Before the application controller is loaded, the HTTP request and any user submitted data is filtered for security.
The Controller loads the model, core libraries, helpers, and any other resources needed to process the specific request.
The finalized View is rendered then sent to the web browser to be seen. If caching is enabled, the view is cached first so that on subsequent requests it can be served.


45) How do you set default timezone in codeigniter?

We can do by adding date_default_timezone_set(‘America/LosAngeles’); in index.php


46) Who developed codeigniter?

Codeigniter was developed bt ellislab Inc.


47) Why codeigniter is called as loosely based mvc framework?

Reason behind this is we does not need to follow strict mvc pattern while creating application.We can also able to build with model only view and controllers are enough to built a application.


48) How do I do a COUNT(‘foo’) using the Active Record functions?

You need to use the SQL AS feature, where you assign a new name to a piece of data. For example: $this->db->select(“COUNT(‘foo’) AS foo_count”, FALSE);// Run your query, and then use the


49) Can I cache only certain parts of a page?

This is related to the question above about nested templates and partials. Basically, CI cache library (1.5.4) only supports full page caching – it’s all or nothing. There are several


50) Is there a way to cycle $this->input->post() items?

There are no CodeIgniter functions to do this, but you can accomplish this easily with a construct such as this one. The result of this function is a $safe_post_array that contains all posted data.


 

Leave a Comment