Codeigniter Interview Questions and Answers

Codeigniter Interview Questions and Answers

1. what is CodeIgniter?

Ans. Codeigniter is an open source framework for web application. It is used to develop websites on PHP. It is loosely based on MVC pattern, and it is easy to use compare to other PHP framework.

2. what are hooks in CodeIgniter?

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

3. Explain how you will load or add a model in CodeIgniter?

Ans. Within your controller functions, models will typically be loaded; you will use the function$this->load->model (‘Model_Name’);

4. Explain how you can prevent CodeIgniter from CSRF?

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

5.Explain how you can extend the class in Codeigniter?

Ans. To extend the native input class in CodeIgniter, you have to build a file named application/core/MY_Input.php and declare your class withClass MY_Input extends CI_Input {}

6.Mention what is the default URL pattern used in Codeigniter framework?

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

7.Explain what is inhibitor in CodeIgniter?

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

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

Ans. 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/566.gpg.

9.Mention what are the security parameter for XSS in CodeIgniter?

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

10.List out different types of hook point in Codeigniter?

Ans. Different types of hook point in Codeigniter includespost_controller_constructor
pre_controller
post_sytem
pre_system
cache_override
display_override
post_controller

12.Why is there a need to configure the URL routes?

Ans. Why is there a need to configure the URL routes?

13. Explain routing in Codeigniter?

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

14.what helpers in CodeIgniter are and how you can load a helper file?

Ans. 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’) ;

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

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

16.Explain MVC in CodeIgniter.

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

17.How can you connect models to a database manually?

Ans. To connect database manually use following syntax,$this->load->database();

18.How can you add or load a model in CodeIgniter?

Ans. To load models in controller functions, use the following function:$this->load->model(‘ModelName’);If in case your model file is located in sub-directory of the model folder, then you have to mention the full path. For example, if your file location is application/controller/models/project/ModelName. Then, your file will be loaded as shown below :-
$this->load->model(‘project/ModelName’);

19.Explain controller in CodeIgniter.

Ans. 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/ n this URI, CodeIgniter try to find Front.php file and Front class.

20. What is the default method name in CodeIgniter?

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

21.How can you extend a class in CodeIgniter?

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

22. What is a helper in CodeIgniter? How can a helper file be loaded?

Ans. Helpers are the group of functions that are used to assist the user to perform specific tasks.URL Helpers: used to create the links.Text Helpers: used for text formatting.Cookies Helpers: used for reading and setting cookies.

23. What are the most prominent features of CodeIgniter?

Ans. 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 the Model View Controller (MVC) pattern.
It has full featured database classes and support for several platforms.
It is extensible. You can easily extend the system by using your libraries, helpers.
Excellent documentation.

24. How will you call a constructor in CodeIgniter?

Ans. To use a constructor, you need to mention the following line of code,
parent::_construct()

25. Explain the difference between helper and library in CodeIgniter?

Ans. Helper is a set of Common functions which we can use within Models, Views, Controllers everywhere. Once we include that file then we can get access to the functions.Library is a class which we need to make an instance of the class by $this->load->library() this function.NOTE : A library is used in object-oriented context but a helper is more suitable to be used within the Views

26. What do you mean by Get_instance in CodeIgniter?

Ans. It is a globally available methods or functions that returns the Controller super-object which contains all the currently loaded classes. get_instance() function returns the Controller class instance.

27. How to remove index.php from URL in Codeigniter?

Ans. You can follow these given steps to remove index.php from URL in Codeigniter.
Please open config.php and change from $config[‘index_page’] = “index.php” to $config[‘index_page’] = “”;
Update also from $config[‘uri_protocol’] =”AUTO”; to $config[‘uri_protocol’] = “REQUEST_URI”;
Put this code in your htaccess file
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]

28. What is Codeigniter Library and how we can load it?

Ans. Codeigniter provides a rich set of libraries, which indirectly increase the speed of developing an application. These Codeigniter system library is located in system/libraries.In Codeigniter the library can be loaded with $this->load->library(‘class name’);If we want to load multiple libraries then we can use like $this->load->library(array(‘comment’, ‘blog’));

29. How to handle sessions in Codeigniter?

Ans. How to add values in session in Codeigniter$_SESSION[‘username’] = ‘bestinterviewquestion.com’ // It can be use in core PHP
$this->session->set_userdata(‘username’, ‘bestinterviewquestion.com’); // It can be done in CodeigniterWe can pass array to store values in session in Codeigniter.$array = array(
‘username’ => ‘bestinterviewquestion.com’,
’email’ => ‘info@bestinterviewquestion.com’,
‘url’ => ‘https://www.bestinterviewquestion.com’
);$this->session->set_userdata($array);
How to remove values in session in Codeigniter
unset($_SESSION[‘username’]); // It can be use in core PHP
$this->session->unset_userdata(‘username’); // It can be done in Codeigniter

If we want to remove more values from session then we can use like this

$this->session->unset_userdata($array); // $array is defined above. It is an array with key & values.

How to get data from session in Codeigniter
$this->session->userdata(‘username’); // Here we will get username, if it is exists in session.

30. Explain why codeigniter is called as loosely based mvc framework?

Ans. It is called as loosely based mvc framework because in codeigniter controller is the necessary but model and view are optional. It means we can build a website without model.

31. How to install CodeIgniter? Explain step by step.

Ans. CodeIgniter can be installed with four steps that are given below:
Download it from its download page & then unzip the package.
Upload its files and folders on your server or localhost.
Open application/config/config.php file and set base URL.
If you want to use database then open application/config/database.php file and set your database credientails.
Now it’s installed

32. What are the server requirements to install codeigniter?

Ans. We need only PHP version 5.6 or greater than 5.6. because of potential security and performance issues.

33. How we can load multiple helper files in Codelgniter?

Ans. You have to pass your helpers in array form. Here helper_1, helper_2, helper_3, helper_4 are different helpers that we want to load.
$this->load->helper(array(‘helper_1’, ‘helper_2’, ‘helper_3’, ‘helper_4’));

34. How we can print SQL statement in codeigniter model?

Ans. You can use $this->db->last_query(); to display the query string.
You can use print_r($query); to display the query result.

35. Explain how to create custom 404 page in CodeIgniter?

Ans. You can create your custom 404 page with these steps. Please follow these steps one by by.
You have to update your own created controller’s class with $route[‘404_override’] = ‘Controller_Class_Name’;. You have to update this in your application/config/routes.php
Please create your new controller “Controller_Class_Name” file in your controllers directory application/controllers/.
Please put this code in your Controller_Class_Name.phpclass Controller_Class_Name extends CI_Controller {
public function __construct() {
parent::__construct();
}public function index() {
$this->output->set_status_header(‘404’);
$this->load->view(‘error_page_404‘);
}
}
Now create your “error_page_404” view file.

36. How to get last inserted id in codeigniter?

Ans. You can use $lastInsertID = $this->db->insert_id(); after insert query.

1. what is CodeIgniter?

Ans. Codeigniter is an open source framework for web application. It is used to develop websites on PHP. It is loosely based on MVC pattern, and it is easy to use compare to other PHP framework.

2. what are hooks in CodeIgniter?

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

3. Explain how you will load or add a model in CodeIgniter?

Ans. Within your controller functions, models will typically be loaded; you will use the function$this->load->model (‘Model_Name’);

4. Explain how you can prevent CodeIgniter from CSRF?

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

5.Explain how you can extend the class in Codeigniter?

Ans. To extend the native input class in CodeIgniter, you have to build a file named application/core/MY_Input.php and declare your class withClass MY_Input extends CI_Input {}

6.Mention what is the default URL pattern used in Codeigniter framework?

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

7.Explain what is inhibitor in CodeIgniter?

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

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

Ans. 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/566.gpg.

9.Mention what are the security parameter for XSS in CodeIgniter?

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

10.List out different types of hook point in Codeigniter?

Ans. Different types of hook point in Codeigniter includespost_controller_constructor
pre_controller
post_sytem
pre_system
cache_override
display_override
post_controller

12.Why is there a need to configure the URL routes?

Ans. Why is there a need to configure the URL routes?

13. Explain routing in Codeigniter?

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

14.what helpers in CodeIgniter are and how you can load a helper file?

Ans. 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’) ;

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

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

16.Explain MVC in CodeIgniter.

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

17.How can you connect models to a database manually?

Ans. To connect database manually use following syntax,$this->load->database();

18.How can you add or load a model in CodeIgniter?

Ans. To load models in controller functions, use the following function:$this->load->model(‘ModelName’);If in case your model file is located in sub-directory of the model folder, then you have to mention the full path. For example, if your file location is application/controller/models/project/ModelName. Then, your file will be loaded as shown below :-
$this->load->model(‘project/ModelName’);

19.Explain controller in CodeIgniter.

Ans. 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/ n this URI, CodeIgniter try to find Front.php file and Front class.

20. What is the default method name in CodeIgniter?

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

21.How can you extend a class in CodeIgniter?

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

22. What is a helper in CodeIgniter? How can a helper file be loaded?

Ans. Helpers are the group of functions that are used to assist the user to perform specific tasks.URL Helpers: used to create the links.Text Helpers: used for text formatting.Cookies Helpers: used for reading and setting cookies.

23. What are the most prominent features of CodeIgniter?

Ans. 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 the Model View Controller (MVC) pattern.
It has full featured database classes and support for several platforms.
It is extensible. You can easily extend the system by using your libraries, helpers.
Excellent documentation.

24. How will you call a constructor in CodeIgniter?

Ans. To use a constructor, you need to mention the following line of code,
parent::_construct()

25. Explain the difference between helper and library in CodeIgniter?

Ans. Helper is a set of Common functions which we can use within Models, Views, Controllers everywhere. Once we include that file then we can get access to the functions.Library is a class which we need to make an instance of the class by $this->load->library() this function.NOTE : A library is used in object-oriented context but a helper is more suitable to be used within the Views

26. What do you mean by Get_instance in CodeIgniter?

Ans. It is a globally available methods or functions that returns the Controller super-object which contains all the currently loaded classes. get_instance() function returns the Controller class instance.

27. How to remove index.php from URL in Codeigniter?

Ans. You can follow these given steps to remove index.php from URL in Codeigniter.
Please open config.php and change from $config[‘index_page’] = “index.php” to $config[‘index_page’] = “”;
Update also from $config[‘uri_protocol’] =”AUTO”; to $config[‘uri_protocol’] = “REQUEST_URI”;
Put this code in your htaccess file
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]

28. What is Codeigniter Library and how we can load it?

Ans. Codeigniter provides a rich set of libraries, which indirectly increase the speed of developing an application. These Codeigniter system library is located in system/libraries.In Codeigniter the library can be loaded with $this->load->library(‘class name’);If we want to load multiple libraries then we can use like $this->load->library(array(‘comment’, ‘blog’));

29. How to handle sessions in Codeigniter?

Ans. How to add values in session in Codeigniter$_SESSION[‘username’] = ‘bestinterviewquestion.com’ // It can be use in core PHP
$this->session->set_userdata(‘username’, ‘bestinterviewquestion.com’); // It can be done in CodeigniterWe can pass array to store values in session in Codeigniter.$array = array(
‘username’ => ‘bestinterviewquestion.com’,
’email’ => ‘info@bestinterviewquestion.com’,
‘url’ => ‘https://www.bestinterviewquestion.com’
);$this->session->set_userdata($array);
How to remove values in session in Codeigniter
unset($_SESSION[‘username’]); // It can be use in core PHP
$this->session->unset_userdata(‘username’); // It can be done in Codeigniter

If we want to remove more values from session then we can use like this

$this->session->unset_userdata($array); // $array is defined above. It is an array with key & values.

How to get data from session in Codeigniter
$this->session->userdata(‘username’); // Here we will get username, if it is exists in session.

30. Explain why codeigniter is called as loosely based mvc framework?

Ans. It is called as loosely based mvc framework because in codeigniter controller is the necessary but model and view are optional. It means we can build a website without model.

31. How to install CodeIgniter? Explain step by step.

Ans. CodeIgniter can be installed with four steps that are given below:
Download it from its download page & then unzip the package.
Upload its files and folders on your server or localhost.
Open application/config/config.php file and set base URL.
If you want to use database then open application/config/database.php file and set your database credientails.
Now it’s installed

32. What are the server requirements to install codeigniter?

Ans. We need only PHP version 5.6 or greater than 5.6. because of potential security and performance issues.

33. How we can load multiple helper files in Codelgniter?

Ans. You have to pass your helpers in array form. Here helper_1, helper_2, helper_3, helper_4 are different helpers that we want to load.
$this->load->helper(array(‘helper_1’, ‘helper_2’, ‘helper_3’, ‘helper_4’));

34. How we can print SQL statement in codeigniter model?

Ans. You can use $this->db->last_query(); to display the query string.
You can use print_r($query); to display the query result.

35. Explain how to create custom 404 page in CodeIgniter?

Ans. You can create your custom 404 page with these steps. Please follow these steps one by by.
You have to update your own created controller’s class with $route[‘404_override’] = ‘Controller_Class_Name’;. You have to update this in your application/config/routes.php
Please create your new controller “Controller_Class_Name” file in your controllers directory application/controllers/.
Please put this code in your Controller_Class_Name.phpclass Controller_Class_Name extends CI_Controller {
public function __construct() {
parent::__construct();
}public function index() {
$this->output->set_status_header(‘404’);
$this->load->view(‘error_page_404‘);
}
}
Now create your “error_page_404” view file.

36. How to get last inserted id in codeigniter?

Ans. You can use $lastInsertID = $this->db->insert_id(); after insert query.