CODEIGNITER Interview Questions and Answers

CodeIgniter is a free, light weight,fast and Application Development Framework for building web sites using PHP. It is based on MVC i.e. Model-View-Controller design pattern.It helps us to focus on project by minimizing the amount of code needed for given task..


1) What is the current version of Codeigniter and how to check the version of CodeIgniter framework?

The current version of the CodeIgniter framework is 3.1.10. We can check the version of codeigniter framework in system/core/CodeIgniter.php file, where you can check CI_VERSION constant value as define(‘CI_VERSION’, ‘3.1.10’);


2) What is the default URL pattern used in Codeigniter framework?

In CodeIgniter, URLs are designed to be search-engine and human-friendly.CodeIgniter uses a segment-based approach rather than using “query string” based approach.

abc.com/user/edit/id

The default URL pattern in CodeIgniter consists of 4 main components. They are :

  1. A server name (abc.com)
  2. A Controller (user)
  3. An Action or method (edit)
  4. An optional action parameter (id)


3) Explain helpers in CodeIgniter and how to load helper file?

As the name suggests, helpers help you with tasks.Each helper file is simply a collection of functions in a particular category.They are simple, procedural functions.Each helper function performs one specific task, with no dependence on other functions. CodeIgniter does not load Helper Files by default, so the first step in using a Helper is to load it. Once loaded, it becomes globally available in your controller and views. Helpers are typically stored in your system/helpers, or application/helpers directory Loading a helper file is quite simple using the following method:

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

Where name is the file name of the helper, without the .php file extension.

Loading Multiple Helpers:

If you need to load more than one helper you can specify them in an array, like this:

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


4) What are the 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.How ever you like to cause some action to take place at a particular stage in the execution process.

The hooks feature can be globally enabled/disabled by setting the following item in the application/config/config.php file:

$config[‘enable_hooks’] = TRUE;

Hooks are defined in application/config/hooks.php file.

For example:

  1.  $hook['pre_controller'] = array(
  2. 'class' => 'MyClass',
  3. 'function' => 'Myfunction',
  4. 'filename' => 'Myclass.php',
  5. 'filepath' => 'hooks',
  6. 'params' => array('param1', 'param2', 'param3')
  7. ); 


5) How you will add or load an model in codeigniter?

Models will typically be loaded and called from within your controller functions. To load a model you will use the following function:

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


6) How do you get last inserted ID in Codeigniter?

In CodeIgniter insert_id() method is used to get last insert id.

Syntax: $this->db->insert_id();

Usage : 

function add_post($post_data){
   $this->db->insert('posts', $post_data);
   $insert_id = $this->db->insert_id();
   return  $insert_id;
}


7) How to print SQL statement in codeigniter model?

In codeigniter, to print query in model we use : 

$this->db->last_query();


8) What are the different types of hook are there in Codeigniter?

The following is a list of available 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 immediately prior to any of your controllers being called. All base classes, routing, and security checks have been done.
  • post_controller_constructor Called immediately after your controller is instantiated, but prior to any method calls happening.
  • post_controller Called immediately after your controller is fully executed.
  • display_override 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().
  • cache_override 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.
  • post_system 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.


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

$this->load->database();


10) Explain MVC in Codeigniter?

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 has a fairly loose approach to MVC since Models are not required. If you don’t need the added separation or find that maintaining models requires more complexity than you want, you can ignore them and build your application minimally using Controllers and Views. CodeIgniter also enables you to incorporate your own existing scripts, or even develop core libraries for the system, enabling you to work in a way that makes the most sense to you.