CodeIgniter Interview Questions – Set 04

How can you extend a class in CodeIgniter?

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.

Explain controller in CodeIgniter.

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

Consider following URI,

abc.com/index.php/front/
In this URI, CodeIgniter try to find Front.php file and Front class.

How to initialize a driver in CodeIgniter?

To initialize a driver, write the following syntax,

$this->load->driver(‘class_name’);
Here, class_name is the driver name.

How can you load multiple helper files?

To load multiple helper files, specify them in an array,

$this->load->helper(
array(‘helper1’, ‘helper2’, ‘helper3’)
);

How can the CodeIgniter be prevented from CSRF?

There are the various ways by which, we can prevent CodeIgniter from CSRF. The most used method is using the hidden field in each page of the website. The hidden field is stored in the user’s session. The filed is changed with every HTTP request. The user can be detected in its every request to the website. The hidden value is always compared with the one saved in the session. If it is the same, the request is valid.

Explain MVC in CodeIgniter.

CodeIgniter framework is based on MVC pattern. MVC is a software that gives you a separate logical view from the presentation view. Due to this, a web page contains minimal scripting.

Model – The Controller manages models. It represents your data structure. Model classes contain functions through which you can insert, retrieve or update information in your database.
View – View is the information that is presented in front of users. It can be a web page or parts the page like header and footer.
Controllers – Controller is the intermediary between models and view to process HTTP request and generates a web page. All the requests received by the controller are passed on to models and view to process the information.

What is routing in CodeIgniter?

Routing is a technique by which you can define your URLs according to the requirement instead of using the predefined URLs. Routes can be classified in two ways, either using Wildcards or Regular Expressions.

Wildcards
There are two types of wildcards:

:num−series containing only numbers matched.
:any−series containing only characters matched.
Regular Expression
Regular expressions are also used to redirect routes.

$route[‘blog'(a-zA-Z0-9]+)’] = ‘women/social’;
You can create your regular expression to run your URL.