Codeigniter Interview Question and Answer

Our Codeigniter Interview Question covers following topic like, CodeIgniter Basic,CodeIgniter framework functionality, how CodeIgniter Framework works, CodeIgniter security, CodeIgniter session, CodeIgniter Helpers,Error handling in codeigniter, Codeigniter Routing.

Q.What is Codeigniter?

A.CodeIgniter is PHP Open Source php MVC framework that can help us to speed up the development of our web applications.

Q.What are the hooks in codeigniter?

A.CodeIgniter's Hooks feature allow us to change the inner working of the framework without changing the core files.In Simple words, hooks allow us to execute a script with a particular path within the Codeigniter.For Example you need to check user is logged in or not before the execution of some controller.Here you can use hook and save your time to write code multiple times. Hooks Files is located in following place in Codeigniter. More Details...

Q.How to load a model in CodeIgniter?

A.To load models in controller ,we will use the following function. $this->load->model('ModelName');

Q.How to connect models to a database manually?

A.To connect models to a database manually ,we will use following syntax. $this->load->database();

Q.How to load a view in CodeIgniter?

A.To load a view in CodeIgniter ,we will use following syntax. $this->load->view('view_page_name');

Q.What is Controller in CodeIgniter?

A. Controller in CodeIgniter is class file.Controller is bridge between models and views to process HTTP request and generates a web page.

Consider following URI

tutsway.com/index.php/home/

In above example, CodeIgniter would attempt to find a controller named Home.php and load it.

Controller Syntax in CodeIgniter

<?php defined('BASEPATH') OR exit('No direct script access allowed'); class Home extends CI_Controller { public function index() { $this->load->view('home'); } }

Q.How to call a constructor in CodeIgniter?

A.To call a constructor in CodeIgniter, we will use following syntax. $Parent::__construct() ;

We need to call the parent constructor manually because local constructor will be overriding the one in the parent controller.

Q.Explain inhibitor in CodeIgniter?

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

Q.What is the default method name in CodeIgniter?

A.By default every controller calls index method.If you want to call a different method, then write it in the controller file like below. <?php defined('BASEPATH') OR exit('No direct script access allowed'); class Home extends CI_Controller { public function newfunction() { echo "hello world." } }

To run this program on browser, type following Url in web browser. http://localhost/CodeIgniter/index.php/Home/newFunction

Q.How to connect multiple databases in CodeIgniter?

A.To connect more than one database in CodeIgniter, do the following,

$db1 = $this->load->database('mydb1', TRUE); $db1 = $this->load->database('mydb2', TRUE); More Details...

Q.What is CodeIgniter Helper ?

A.Codeigniter Helpers,as the name suggests help us to do task.CodeIgniter helper file is simply a collection of functions of particular category.CodeIgniter Comes with more than 20 system helpers.

More Details...

Q.How to load multiple helper files?

A.To load multiple helper files,Use following syntax. $this->load->helper( array('helper1', 'helper2', 'helper3') );

Q.Where a newly created library is stored in CodeIgniter structure?

A.It should be placed in application/libraries folder.CodeIgniter comes with a rich set of libraries.Which increases the developing speed of an application.

To load multiple libraries, use following code, $this->load->library(array('table', 'email'));

Q.What are different types of hook points in CodeIgniter?

A.The list of hook points is given below.

  • pre_system
  • It is called very early during system execution.Only hook class and benchmark have been loaded at this point.

  • pre_controller
  • It is called before your controller being called. At this point all base classes, security checks and routing have been done.

  • post_controller_constructo
  • It is called immediately after your controller is instantiated, but before any method call.

  • post_controller
  • It is called immediately after your controller is fully executed.

  • display_override
  • It is used to send the final page at the end of file execution.

  • cache_override
  • It enables you to call your own method instead of the _display_cache() method in the Output Library.

  • post_system
  • It is called end of system execution after the finalized data is sent to the browser.

Q.How can you print SQL statement in CodeIgniter model?

A.echo $this->db->last_query();

Q.How can you extend class in CodeIgniter?

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

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

A.To Enable CSRF (Cross Site Request Forgery) protection in CodeIgniter go to "application/config/config.php". $config [ ‘csrf_protection’] = TRUE;

If you are using to form helper, then form_open() function will insert a hidden csrf field in your forms automatically.

Q.How do you set default timezone in codeigniter?

A.We can set default timezone in codeigniter,using following syntax in index.php. date_default_timezone_set(‘America/LosAngeles’);

Q.How to unset session in codeigniter?

A.To unset session in codeigniter,use following syntax. $this->session->unsetuserdata('somename');

More Details...

Q. How do you get last insert id in codeigniter?

A.Use following syntax to get last insert id. $this->db->insertid();

Q.How to access config variable in codeigniter?

A.To access config variable in codeigniter,use following syntax. $this->config->item('variable name');

Q.Explain Routing in codeigniter?

A. In Codeigniter, PHP files served is different rather than accessing it directly from the browser.We called this process routing.Routing gives us freedom to modify the default URL to custom URL pattern according to the requirement. So, whenever there is a request made and matches our URL pattern (Defined in route file) it will direct to the specified controller and function .

Q.How to print SQL statement in codeigniter?

A. echo $this->db->lastquery();

Q.Explain what is inhibitor in CodeIgniter?

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

Q.How you will work with error handling in CodeIgniter?

A.CodeIgniter uses following function for error reporting in to your applications.CodeIgniter has an error logging class that permits error and debugging messages to be saved as text files.

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

show_error function will display the error message supplied to it using template "application/errors/error_general.php".

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

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

log_message(‘level’, ‘message’)

log_message function allow you write messages to your log files.

if ($some_variable == "") { log_message('error', 'Variable did not contain a value.'); } else { log_message('debug', 'Variable was correctly set'); } log_message('info', 'Variable is to provide some value.');

Note:You must supply one of three "levels" in the first parameter (debug, error, info), with the message itself in the second parameter.The "logs" folder must be writable.

Q.How codeigniter join 2 tables?

A.In Codeigniter join() method is used to join 2 or more tables in codeigniter. $this->db->select('*'); $this->db->from('employee'); $this->db->join('category', 'category.id = articles.id'); $query = $this->db->get();