CodeIgniter Interview Questions: CodeIgniter Interview Questions and Answers. CodeIgniter is an open-source, rapid development web framework, for use in building dynamic websites written in PHP.
Codeigniter Interview Questions

CodeIgniter | CodeIgniter Interview Questions | Basic

Q:- What is CodeIgniter?
  • Codeigniter is a light-weight, open source, php framework. Codeigniter is loosely based on MVC pattern.
  • CodeIgniter is the most simplest framework in PHP, which is you will easily learn.
  • It's mostly known for its speed as compared to other frameworks in php.

Its goal is to enable you to develop projects much faster than you could if you were writing code from scratch, by providing a rich set of libraries for commonly needed tasks, as well as a simple interface and logical structure to access these libraries.

Q:- When and who developed codeigniter?

CodeIgniter was created by EllisLab and is now a project of the British Columbia Institute of Technology. The first public version of CodeIgniter was released on February 28, 2006.

Q:- Which is the best PHP ORM for codeigniter?
DataMapper

An Object Relational Mapper written in PHP for CodeIgniter. It is designed to map your Database tables into easy to work with objects, fully aware of the relationships between each other.

Gas ORM

A lightweight and easy-to-use ORM for CodeIgniter. Gas was built specifically for CodeIgniter app. It uses CodeIgniter Database packages, a powerful DBAL which support numerous DB drivers. Gas ORM provide a set of methods that will map your database tables and its relationship, into accessible object.

Doctrine
Doctrine Link
NOTE: You must do some workaround to integrate this with CI, try here.
You may also like - Core PHP Interview Questions
Q:- What are the features of codeigniter?
Following are the features of CodeIgniter:
  • It is free to use as for open source framework.
  • It is light weight. The core system requires only a few very small libraries. Not like other frameworks that require heavy file libraries.
  • CodeIgniter is Fast. It is faster than any other framework in php.
  • The URLs generated by CodeIgniter are clean and search engine friendly. You will change any url to what ever you want from files.
  • CodeIgniter is Extensible. The system can be easily extended through the use of your own libraries, helpers, or through class extensions or system hooks.
  • CodeIgniter Uses MVC (Model View Controller) which allows great separation between logic and presentation.
  • CodeIgniter requires nearly zero configuration, does not require you to use the command line, not forced to learn a templating language.
  • Full Featured database classes with support for several platforms, Security and XSS Filtering, Error Logging.
Q:- Helper vs. Library: CodeIgniter?
Helpers
are just small functions that help you avoid repetitive code and produce standard results. Whereas libraries contain classes, can include different files, talk to database etc.
Library
is used in object oriented context (Controller, …), while a helper is more suitable to be used within the Views (non object oriented).
Helper, Plugin and Library:

Since all three methods achieve the same ends. The question then is when do you use what?

Fortunately, CI has also provided that distinction in their user guide which you can go read it yourself.

For me, these are my guidelines on when to use what:
1. Plugins –
I put all 3rd party codes I’m using in my application as Plugins. I would, as best as I can, try to use classes rather than straight function calls.
2. Helpers –
Any standalone straight functions calls, which are repetitive in nature, I classify them as Helpers. For example, sorting functions, my own calculation functions, etc
3. Libraries –
I classify my own classes as ‘Libraries’. Normally, if I’m already writing a class in my application, it would then to be the core logic of the application, as such, I group them all in the Library folder.
Q:- Explain Application Flow Chart in codeigniter?
Application flow chart from Codeigniter documentaion
  1. The index.php serves as the front controller, initializing the base resources needed to run CodeIgniter.
  2. The Router examines the HTTP request to determine what should be done with it.
  3. If a cache file exists, it is sent directly to the browser, bypassing the normal system execution.
  4. Security. Before the application controller is loaded, the HTTP request and any user submitted data is filtered for security.
  5. The Controller loads the model, core libraries, helpers, and any other resources needed to process the specific request.
  6. The finalized View is rendered then sent to the web browser to be seen. If caching is enabled, the view is cached first so that on subsequent requests it can be served.
Q:- 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.

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.

You may also like - CakePHP Interview Questions

CodeIgniter | CodeIgniter Interview Questions | Advanced

Q:- 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
$hook['pre_controller'] = array( 'class' => 'MyClass', 'function' => 'Myfunction', 'filename' => 'Myclass.php', 'filepath' => 'hooks', 'params' => array('param1', 'param2', 'param3') );
Q:- What are the different types of hook in Codeigniter?
  • post_controller_constructor
  • pre_controller
  • pre_sytem
  • post_sytem
  • cache_override
  • display_override
  • post_controller
You may also like - Top 50 Laravel Interview Questions
Q:- What are the helpers in codeigniter?
Helpers, as the name suggests, help you with tasks. Each helper file is simply a collection of functions in a particular category.

There are many inbuilt helpers few of them are:

  • URL Helpers
  • Form Helpers
  • Text Helpers
  • Cookie Helpers
  • File Helpers

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.

CodeIgniter will look first in your application/helpers directory. If the directory does not exist or the specified helper is not located there CI will instead look in your global system/helpers folder.

Loading a Helper

Loading a helper file is quite simple using the following function:

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

Where helper_name is the file name of the helper, without the .php file extension or the "helper" part.

For example, to load the URL Helper file, which is named url_helper.php, you would do this:

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

A helper can be loaded anywhere within your controller functions (or even within your View files, although that's not a good practice), as long as you load it before you use it.

You can load your helpers in your controller constructor so that they become available automatically in any function, or you can load a helper in a specific function that needs it.

Note: The Helper loading function above does not return a value, so don't try to assign it to a variable. Just use it as shown.

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') );
Auto-loading Helpers
If you find that you need a particular helper globally throughout your application, you can tell CodeIgniter to auto-load it during system initialization. This is done by opening the application/config/autoload.php file and adding the helper to the autoload array.
Using a Helper

Once you've loaded the Helper File containing the function you intend to use, you'll call it the way you would a standard PHP function.

For example, to create a link using the anchor() function in one of your view files you would do this:
echo anchor('blog/comments', 'Click Here');

Where "Click Here" is the name of the link, and "blog/comments" is the URI to the controller/function you wish to link to.

"Extending" Helpers

To "extend" Helpers, create a file in your application/helpers/ folder with an identical name to the existing Helper, but prefixed with MY_ (this item is configurable. See below.).

For example, to extend the native Array Helper you'll create a file named application/helpers/MY_array_helper.php, and add or override functions:

// any_in_array() is not in the Array Helper, so it defines a new function function any_in_array($needle, $haystack) { $needle = (is_array($needle)) ? $needle : array($needle); foreach ($needle as $item) { if (in_array($item, $haystack)) { return TRUE; } } return FALSE; }
// random_element() is included in Array Helper, so it overrides the native function function random_element($array) { shuffle($array); return array_pop($array); } Setting Your Own Prefix The filename prefix for "extending" Helpers is the same used to extend libraries and Core classes. To set your own prefix, open your application/config/config.php file and look for this item: $config['subclass_prefix'] = 'MY_';
Please note that all native CodeIgniter libraries are prefixed with CI_ so DO NOT use that as your prefix.
Q:- How you will use or add codeigniter libraries?
All of the available libraries are located in your system/libraries folder.

In most cases, to use one of these classes involves initializing it within a controller using the following initialization function:

$this->load->library('classname');

Where class name is the name of the class you want to invoke.

For example, to load the form validation class you would do this:

$this->load->library('form_validation');

Once initialized you can use it as indicated in the user guide page corresponding to that class.

Additionally, multiple libraries can be loaded at the same time by passing an array of libraries to the load function.

$this->load->library(array('emailtable'));
Q:- How you will work with error handling in codeigniter?
CodeIgniter lets you build error reporting into your applications using the functions:-
1. show_error()
This function will display the error message supplied to it using template application/errors/error_general.php.
2. show_404()
Function will display the 404 error message.
3. log_message("level", "message")
This function lets you write messages to your log files.

You must supply one of three "levels" in the 1st parameter, indicating what type of message it is (debug, error, info), with the log message itself in the 2nd parameter.

  1. debug
  2. error
  3. info
Q:- Explain what is inhibitor in CodeIgniter?
In CodeIgniter, inhibitor is an error handler class, using the native PHP functions like
  1. set_exception_handler
  2. set_error_handler
  3. register_shutdown_function
  4. register_shutdown_function

to handle parse errors, exceptions, and fatal errors.

Q:- Explain routing in Codeigniter?

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

Open the routing file located at application/config/routes.php and add the following two lines.

$route['default_controller'] = "welcome"; // default controller name $route['404_override'] = ''; // 404 page
Q:- Why there is a need to configure the URL routes?
Changing the URL routes has some benefits like
  1. SEO-friendly URLs: From SEO point of view, to make URL SEO friendly and get more user visits Hide some URL element such as a function name, controller name, etc.
  2. Security: from the users for security reasons Provide different functionality to particular parts of a system.
Q:- How to set & unset session variable in codeigniter?
Set Single Value:
$this->session->set_userdata('SESSION_NAME', 'CodeIgniter Interview Questions and Answers');
Set Multiple Value:
$newdata = array( 'username' => 'FullStackTutorials', 'email' => 'fullstacktutorials@gmail.com', 'logged_in' => TRUE ); $this->session->set_userdata($newdata);
Verify session value:

If you want to verify that a session value exists, simply check with has_userdata()

$this->session->has_userdata('SESSION_NAME');
Unset Session:
$this->session->unset_userdata('SESSION_NAME'); unset multiple thing at in single call $array_items = array('username', 'email'); $this->session->unset_userdata($array_items);
Flashdata:

CodeIgniter supports "flashdata", or session data that will only be available for the next request, and is then automatically cleared.

This can be very useful, especially for one-time informational, error or status messages (for example: "N Record deleted").

To add flashdata:
$this->session->set_flashdata('item', 'value');

However, if you want to be sure that you’re reading "flashdata" (and not any other kind), you can also use the flashdata() method:

$this->session->flashdata('item'); Or to get an array with all flashdata, simply omit the key parameter: $this->session->flashdata();
To mark an existing item as "flashdata":
$this->session->mark_as_flash('item');

If you want to mark multiple items as flashdata, simply pass the keys as an array:

$this->session->mark_as_flash(array('item', 'item2'));
Preserve flashdata:

If you find that you need to preserve a flashdata variable through an additional request, you can do so using the keep_flashdata() method. You can either pass a single item or an array of flashdata items to keep.

$this->session->keep_flashdata('item'); $this->session->keep_flashdata(array('item1', 'item2', 'item3'));
Tempdata:

CodeIgniter also supports "tempdata", or session data with a specific expiration time. After the value expires, or the session expires or is deleted, the value is automatically removed.

To mark an existing item as "tempdata", simply pass its key and expiry time (in seconds!) to the mark_as_temp() method:

// 'item' will be erased after 300 seconds $this->session->mark_as_temp('item', 300); You can mark multiple items as tempdata in two ways, depending on whether you want them all to have the same expiry time or not: // Both 'item' and 'item2' will expire after 300 seconds $this->session->mark_as_temp(array('item', 'item2'), 300); // 'item' will be erased after 300 seconds, while 'item2' // will do so after only 240 seconds $this->session->mark_as_temp(array( 'item' => 300, 'item2' => 240 ));
To add tempdata:
$_SESSION['item'] = 'value'; $this->session->mark_as_temp('item', 300); // Expire in 5 minutes Or alternatively, using the set_tempdata() method: $this->session->set_tempdata('item', 'value', 300); You can also pass an array to set_tempdata(): $tempdata = array('newuser' => TRUE, 'message' => 'Thanks for joining!'); $this->session->set_tempdata($tempdata, NULL, $expire);
Note:
  1. If the expiration is omitted or set to 0, the default time-to-live value of 300 seconds (or 5 minutes) will be used.
  2. The userdata() method will NOT return flashdata items.
  3. The userdata() method will NOT return tempdata items.
Destroying a Session:

To clear the current session (for example, during a logout), you may simply use either PHP’s session_destroy() function, or the sess_destroy() method. Both will work in exactly the same way:

session_destroy(); OR $this->session->sess_destroy();
Q:- Mention what is the default URL pattern used in Codeigniter framework?
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.

#Example:http://serverName/controllerName/controllerFunction/parameter1/parameter2. GET URI SEGMENT IN CODEIGNITER: $this->uri->segment(1); // controller name $this->uri->segment(2); // action name $this->uri->segment(3); // 1st segment $this->uri->segment(4); // 2nd segment
Q:- Question: How to access config variable in codeigniter?
$this->config->item('VARIABLE_NAME');
Q:- Explain how you can enable CSRF (Cross Site Request Forgery) in CodeIgniter?

Go to the path - application/config/config.php

$config['csrf_protection'] = TRUE;

The CSRF token is added to the form as a hidden input only when the form_open() function is used.

A cookie with the CSRF token's value is created by the Security class, and regenerated if necessary for each request.

If $_POST data exists, the cookie is automatically validated by the Input class. If the posted token does not match the cookie's value, CI will show an error and fail to process the $_POST data.

So basically, it's all automatic - all you have to do is enable it in your $config['csrf_protection'] and use the form_open() function for your form.

CodeIgniter | CodeIgniter Interview Questions | Database

You may also like - MySQL Interview Questions
Q:- What is Model in CodeIgniter?
Model’s responsibility is to handle all data logic and representation and load data in the views. It is stored in application/models folder.
Q:- How to load a model in CodeIgniter?
Your models will typically be loaded and called from within your controller methods. To load a model you will use the following method:
$this->load->model('model_name');
If your model is located in a sub-directory, include the relative path from your models directory.

For example, if you have a model located at application/models/blog/Queries.php you'll load it using:

$this->load->model('blog/queries');
Once loaded, you will access your model methods using an object with the same name as your class:
$this->load->model('model_name'); $this->model_name->method();
If you would like your model assigned to a different object name you can specify it via the second parameter of the loading method:
$this->load->model('model_name', 'foobar'); $this->foobar->method();
Q:- How do you get last inserted ID in Codeigniter?
$this->db->insert_id();
Q:- How to print SQL statement in codeigniter model?
$this->db->last_query();
Q:- How to load helpers in CodeIgniter?
$this->load->helper('helperName');
Q:- How can you connect models to a database manually?
$this->load->database();
You may also like - RESTful Web Services Interview Questions