Top 30 Codeigniter Interview Questions You Must Prepare 19.May.2024

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.

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.

Version: 3.0.5,
Date January 13, 2016

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.

Yes, we can add some extended functionality to a native library by adding one or two methods.

The following graphic illustrates how data flows throughout the system:

CodeIgniter application flow:

  1. The index.php serves as the front controller, initializing the base resources needed to run CodeIgniter.
  2. The Router examines the HTTP request to determine what should be done with it.
  3. If a cache file exists, it is sent directly to the browser, bypassing the normal system execution.
  4. Security. Before the application controller is loaded, the HTTP request and any user submitted data is filtered for security.
  5. The Controller loads the model, core libraries, helpers, and any other resources needed to process the specific request.
  6. 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.

CodeIgniter is based on the Model-View-Controller development pattern. MVC is a software approach that separates application logic from presentation. In practice, it permits your web pages to contain minimal scripting since the presentation is separate from the PHP scripting.

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 View is the information that is being presented to a user.

A View will normally be a web page, but in CodeIgniter, a view can also be a page fragment like a header or footer. It can also be an RSS page, or any other type of “page”.

The Controller serves as an intermediary between the Model, the View, and any other resources needed to process the HTTP request and generate a web page.

Codeigniter was developed by ellislab Inc.

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).

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.

Helpers are the group of functions in a particular category that assist you to perform specific functions.

In CodeIgniter, there are many helpers like:

URL Helpers: helping in creating links.

Text Helpers: perform various text formatting routines.

Cookies Helpers: set and read cookies.

To load helper file, use the following command:

$this->load->model('ModelName');  

following are the folder structure :-

application:

  • cache
  • Config
  • Controllers
  • core
  • errors
  • helpers
  • hooks
  • language
  • libraries
  • logs
  • models
  • thirdparty
  • views

system:

  • core
  • database
  • fonts
  • helpers
  • language
  • libraries

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

Codeigniter is an open source framework for web application on PHP. It is loosely based on MVC pattern and it is similar to CakePHP.

To connect database manually use following syntax,

$this->load->database(); 

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.

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

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.

CodeIgniter provides a rich set of libraries. It is an essential part of CodeIgniter as it increases the developing speed of an application. It is located in the system/library.

It can be loaded as follows,

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

There are three methods to create a library:

  • Creating an entire new library
  • Extending native libraries
  • Replacing native libraries

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

Its structure is as follows,

abc.com/class/function/ID  

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.

  1. Light Weight
  2. CodeIgniter is Extensible
  3. Full Featured database classes

To load models in controller functions, use the following function:

$this->load->model('ModelName');  

CodeIgniter lets you build error reporting into your applications using the functions described below. In addition, it has an error logging class that permits error and debugging messages to be saved as text files.

show_error(‘message’ [, int $statuscode= 500 ] )

This function will display the error message supplied to it using template application/errors/errorgeneral.php.

show_404(‘page’ [, ‘logerror’])

This function will display the 404 error message supplied to it using template application/errors/error404.php.

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.

We can auto load model like this:

$autoload[‘model’] = array(array(‘usersmodel’, ‘users’), array(‘newsmodel’, ‘news’), ‘categorymodel’);

$this->config->item(‘variable name’);

You have to build a file name application/core/MY_Input.php and declare your class with Class MY_Input extends CI_Input {}to extend the native input class in CodeIgniter.

We can use this:

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

$this->session->unsetuserdata(‘somename’);;