Top 10 CodeIgniter Interview Questions & Answer
1. What is Codeigniter Framework?
Codeigniter based on the MVC pattern is an open sourced framework which develops websites for PHP.
Nows a days, Model View Controller(MVC) is the most popular design pattern in software development.
Codeigniter Framework provide predefine function and Library which are used for application development. codeigniter Framework also support HMVC.
2. What is a helper in codeigniter and how it is loaded? Explain different type of helpers?
Helpers is collection of functions which help us with different tasks as name suggested. Every helper function is used to performs specific task with no dependence on other functions.
CodeIgniter provide various types of helper class like url_helper, captcha_helper ,email_helper. All these helpers are located in
system/helper
By default CodeIgniter does not load any Helper files. First step is to be load Helper. After loaded it is available in all controller and views.
To load helpers :
$this->load->helper('name');
To load multiple helpers :
$this->load->helper(array('helper1', 'helper2', 'helper3'));
Different types of helpers are as follows:
- FILE HELPER: It helps to deal with the file.
- TEXT HELPER: This is used to perform various text formatting routines.
- FORM HELPER: It helps in creating form elements.
- COOKIE HELPER: set and real cookies
- URL HELPER: It assists in creating links.
It’s appropriate in the following situations to create a project with CodeIgniter.
- Small learning curve.
- Situations where a framework with a small footprint is needed.
- It’s a framework with zero configuration.
- It’s a framework which doesn’t require to stick to strict coding rules.
- Provides high performance and simple coding structure.
- It’s a framework which doesn’t use the command line.
- It provides built-in protection against CSRF and XSS attacks.
3. What is different between library and helper?
LIBRARY
- The Library is a class. So, the user needs to make an instance of the class to use it.
- Libraries are located under System/Libraries.
- It is object-oriented.
- A library can be loaded using $this->load->library (‘library name’).
- To call library functions, the user needs to create an object of the class.
HELPERS
- A Helper is a file that has PHP functions.
- Helpers are located under System/Helpers.
- These are not written in an object-oriented format, hence the user can simply call helper functions.
- A Helper can be loaded using $this->load->Helper (‘helper name’)
- Helpers can be called in the same way you call PHP functions.
4. How would you use CodeIgniter to connect to multiple databases in a single application?
CodeIgniter allows for multiple database connections within a single application by defining each connection in the application’s database configuration file. To do this, you create an array with the database settings for each connection.
Here is an example of how to define two databases:
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'database_name1',
'dbdriver' => 'mysqli',
);
$db['second_db'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'database_name2',
'dbdriver' => 'mysqli',
);
5. Explain about routing in codeigniter .
CodeIgniter has a userfriendly URI routing rule so that we can re-route URL easily . There is a one-to-one relationship between a URL string and its corresponding controller class and its methods.
Routing is a technique through which we can converts SEO friendly URLs into a server code format that easily understandable.
We can manage these from routes.php file at the located in application/config
6. How to remove index.php from URL in
You can follow these given steps to remove index.php from URL in Codeigniter.
1. Please open config.php
2. change from $config['index_page'] = "index.php" to $config['index_page'] = "";
3. Update also from $config['uri_protocol'] ="AUTO"; to $config['uri_protocol'] = "REQUEST_URI";
4. 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]
7. What is Hooks in CodeIgniter?
Hooks are the special feature of CodeIgniter that allows changing the inner functionality of the framework without making any change in core files of the framework. The hooks are prescribed in application/ hooks.php/config
These are the hook points available in Codeigniter:
- pre_system - It is called during system execution.
- pre_controller - This is called immediately before calling the controllers.
- post_controller - constructor - This method is called instantly after the controller is instantiated but before calling any method.
- post_controller - The post_controller hook is called immediately after the controller is fully executed.
- display_override - This hook overrides the _display() method.
- cache_override - This hook is used when there is a need to call our method instead of the method from Output Library called _display_cache(). It permits us to use your own cache display mechanism.
- post_system - This hook is called when the final page is sent to the browser after the final execution.
8. What is session in codeigniter?
Sessions allows us to maintain the user “states” and helps to track user activity. In codeIgnitor session can be loaded with $this->load->library(‘session’); post loading session library object we can use with $this->session.
To read the session data : $this->session->userdata(‘key’).
Session data can also be get by $this->session->item To create a session in codeIgnitor: Session’s Class set_userdata() method is used to create a session in CodeIgniter. This method takes an associative array containing your data that you want to add in session.
To add userdata one value at a time, set_userdata() also can be use with syntax:
$this->session->set_userdata('some_name', 'some_value');
To remove session data : It can be done with unset a particular key
$this->session->unset_userdata('some_key');
Unset array of item keys :
$array_items = array('username', 'email'); $this->session->unset_userdata($array_items);
9. List the databases supported by Codeigniter Frameworks.
The databases supported by Codeigniter Frameworks are:
- MySQL (5.1+) via the MySQL (deprecated), MySQL and PDO drivers
- Oracle via the oci8 and PDO drivers
- PostgreSQL via the Postgre and PDO drivers
- MS SQL via the MsSQL, Sqlsrv (version 2005 and above only) and PDO drivers
- SQLite via the SQLite (version 2), sqlite3 (version 3) and PDO drivers
- CUBRID via the Cubridand PDO drivers
- Interbase/Firebird via the iBase and PDO drivers
- ODBC via the ODBC and PDO drivers
10. Explian the MVC structure of codeigniter.
MVC stands for MODEL VIEW CONTROLLER. Model View Controller separates application logic from the presentation layer. It helps web pages to contain minimum scripting.
MODEL: It is used to represent data structures. Model classes constitute functions that help to insert, retrieve, and update information in the database.
CONTROLLER: It acts as an intermediary between the view, model, or any other resource to process HTTP requests. The Controller controls the whole application by URI.
VIEW: It represents the information that is being presented to the user. In CodeIgniter, a View could be a simple or complex webpage. The web page contains a header, footer, sidebar, etc. A view cannot be called directly.