Top 50 CodeIgniter Interview Questions & Answers

Top 50 CodeIgniter Interview Questions & Answers

Table of Contents

Top 50 CodeIgniter Interview Questions & Answers – A lot of people who appear for back-end web developer interviews often get quizzed extensively about CodeIgniter. So if you’re looking for a resource on CodeIgniter that will prepare you well for any question that comes your way, you’ve come to the right place!

Top 50 CodeIgniter Interview Questions & Answers

1. What is CodeIgniter?

CodeIgniter is a programming framework that allows the rapid development of highly dynamic websites using the PHP language. It was developed by EllisLab, a software development company, and first released publicly in 2006.

The framework is available as open-source software under the MIT License and follows the model-view-controller (MVC) structure for web development. It also has a very small footprint, taking up very little space on the computer/server.

2. What is the latest version of CodeIgniter?

Although CodeIgniter 3.1.11 is the current version that is used alongside PHP 5.6+, the team is developing CodeIgniter 4, an upcoming version that will be used for web development in PHP 7.2.

Also Read:How to create a professional youtube channel

3. How do you check the version of CodeIgniter you are using?

There are 2 ways to do this. The first is to run the following line of code:

<?php echo CI_VERSION; ?>

The second way is to navigate to the system/core/CodeIgniter.php directory and run the following code:

define(‘CI_VERSION’, ‘2.1.4’);

4. What are the notable features of CodeIgniter?

This is a staple question that features regularly in CodeIgniter interview questions. Here is the list of features you should have at the back of your mind:

  • It is open source under the MIT License
  • It was hailed as the fastest framework for PHP by the creator of PHP
  • It uses the MVC – model, view, controller pattern for development
  • It supports a number of databases and platforms
  • It has easy-to-read-and-use documentation

5. Describe the MVC structure used by CodeIgniter.

MVC stands for model – view – controller, and this is a programming pattern that guides how developers structure their applications when making website back-ends using CodeIgniter.

This pattern allows for very little scripting and a strong, logical overview of the application by separating the software’s logic from its presentation.

  • Model: this encompasses all of your data structures that are directly linked to a particular database. It also contains functions that allow you to retrieve, save, and manipulate data in the database.
  • View: the view, as the name suggests, is everything that is visible to the user. Typically, this refers to a webpage but CodeIgniter also includes entities like headers and footers in the view.
  • Controller: the controller acts as the ‘puppet master’ that manages the seamless integration of models and views by acting as an intermediary and taking care of any other resources needed to process HTTP requests.

6. What is the architecture of CodeIgniter?

There are 3 key details about the architecture of CodeIgniter that you should remember:

  • It is dynamically instantiated making it extremely lightweight
  • The components are mostly independent, making it loosely coupled
  • Every class and function focuses on its own purpose, giving it component singularity

Here is a graphic that neatly shows the architecture:

7. Describe a CodeIgniter ‘model’ in detail.

A lot of CodeIgniter interview questions focus on models. Let’s explore them in detail now.

Models are PHP classes that define how the application interacts with the database. Just like other classes, models contain functions that retrieve, store, insert, and change the information the database contains. These are stored in the application/models folder.

8. What is the basic structure of a model?

This is the bare-bones structure of a model:

class Model_name extends CI_Model {

}

Note that the name starts with an uppercase letter – this is the default convention for the model name. By adding the ‘extends CI_Model’ part, you can make use of all the helper functions that are present in CodeIgniter’s main class.

Here is how you would define a function inside a model:

public function get_last_ten_entries()

{

$query = $this->db->get(‘entries’, 10);

Return $query->result();

}

From this example, you can see how the ‘$query’ command is accessing the database ‘db’ to ‘get’ 10 entries from it and returning it to the function call.

9. How to add/load a model in CodeIgniter?

You will use one of your controller methods to load your model. Use the following code:

$this->load->model(‘model_name’);

If your model is in a sub-directory, replace ‘model_name’ above with the path of the subdirectory. So if your model is in application/models/placeholder/name.php, you will put ‘placeholder/name’ inside the parenthesis.

10. How do you auto-load a model that is to be used globally?

Open the application/config/autoload.php file and add your model to the autoload array. This way, your model will be loaded automatically when the system is initialized.

11. Which databases can be used in CodeIgniter?

These are the databases:

DatabaseDrivers
MySQLMySQL, MYSQLI, PDO
PostgreSQLPostgre, PDO
MS SQLMsSQL, Sqlsrv, PDO
OracleOci8, PDO
SQLiteSQLite, sqlite3, PDO
CUBRIDCubridand, PDO
ODBCODBC, PDO
Interbase/FirebirdiBase, PDO

12. Describe a CodeIgniter ‘view’.

The webpage you are reading this article on is a view. Essentially, a view refers to any web page or a fragment of a web page. This includes nested views in which you have a view within a view and so on.

Views are always loaded by the controller by running the relevant function. Here is the basic structure of a view:

<html>

<head>

<title>My Blog</title>

</head>

<body>

<h1>Welcome to my Blog!</h1>

</body>

</html>

You should save this file in the application/views directory with the ‘.php’ extension.

13. How do you load a view in CodeIgniter?

A view cannot be loaded directly, it must be called by a controller. Here is how to do it:

$this->load->view(‘name’);

There is no need to add ‘.php’ to the ‘name’ of your file, as long as you’re not using any other extension.

Next, you have to add this line to the controller file in the following way:

<?php

class Blog extends CI_Controller {

public function index()

{

$this->load->view(‘blogview’);

}

}

14. Describe a ‘controller’ in CodeIgniter.

A controller is responsible for loading all of your views and executing the functions inside your model classes so that you can retrieve information from the database.

The name of the controller class file is associated directly with a URL that is opened. For example, if you go to the following URL:

something.com/index.php/example

CodeIgniter will search for a controller file named ‘Example.php’ and load its contents. Here is an example of a basic controller that contains a class named ‘Blog’:

<?php

class Blog extends CI_Controller {

public function index()

{

echo ‘Hello World!’;

}

}

Note that the first letter of the class name must always be uppercase.

15. How to define a default controller in CodeIgniter?

When a URL is not present, such as in ‘localhost/codeignitor/’, CodeIgniter will use the default controller. You can set this default controller by navigating to the application/config/routes.php and adjusting the following variable:

$route[‘default controller’] = ‘your_controller’;

Here, ‘your_controller’ is the name of the controller you want to set as default.

16. What is the method to call a constructor in CodeIgnitor?

A constructor can be called using the following line of code:

parent:__construct( )

17. Describe the structure of a CodeIgniter URL.

Following on from its MVC structure, CodeIgniter uses a segment-based approach to URLs instead of the standard query-string approach. Here is an example:

website.com/class/method/ID

Here, the first segment represents the controller to be called, the second represents the specific method from that controller, and the third (and any additional ones) is for variables and IDs to be passed to the method.

18. What is a CodeIgniter inhibitor?

An inhibitor is an error-handler that takes care of errors and faults in your application and processes them in a way that allows you to fix them easily. It formats the error message, puts it in the log, mails it, and redirects the application to a temporary view.

19. What default method does CodeIgniter call?

CodeIgniter will always call ‘index.php’ as the default method unless you have specified something else. You can specify a different method by defining it in the Controller file and calling it explicitly.

20. How do you override the default method through remapping?

You can do this by adding the _remap() function to the Controller file and passing the preferred method you want to load as a parameter. Here is the code for this:

public function _remap($method)

{

if ($method === ‘some method’)

{

$this->method();

}

else

{

$this->default_method();

}

}

Notice that the code will override the default method only if another method is provided in the parameters.

21. Explain what a ‘helper’ refers to in CodeIgniter.

A helper is simply a file that contains useful functions that help make a task easier for you. There are different kinds of helper files:

  • URL helpers that make creating links easier
  • Text helpers that make text editing and manipulation more convenient
  • Cookie helpers which help you manage and read cookies

22. How do you load a helper file?

You load a helper file by adding the following line of code to either the constructor of the controller or inside any function that needs to use it:

$this->load->helper(‘name’);

So if you’re loading the ‘text_helper.php’ file, you should replace the ‘name’ in the code by ‘text’, and you’re good to go.

23. How do you load multiple helper files?

Instead of passing in a single helper in the parameters, you just have to send in an array of helper files. Here is how this is done:

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

24. How do you auto-load a helper file?

Sometimes, when a helper file is needed throughout the application, you can auto-load it by adding the above-mentioned line of code to application/config/autoload.php.

25. What is a library in CodeIgniter? How do you load it?

Libraries are packages made in PHP that provide higher-level abstractions and so make development a lot faster. These remove the need to focus on small, minute details by taking care of those by itself.

To load a library in CodeIgniter, this is what you have to do inside a controller:

$this->load->library(‘class_name’);

26. Where are all libraries located in CodeIgniter?

All the pre-packaged libraries developed by CodeIgniter are available in the system/libraries directory.

27. How do you load multiple libraries at the same time?

The process is similar to loading multiple helper files. You use the same line of code for loading one library but replace the parameter with an array.

$this->load->library(array(‘library1’, ‘library2’));

28. How can you create libraries? What else can you do?

You can create entirely new libraries from scratch inside your application/libraries folder by coding the classes from scratch. However, you can also extend a native library if you want to add some custom functionality to it.

If you find that a native library is not useful and you have a better idea, you can even go ahead and replace it altogether. To summarize, you can:

  • Create new libraries
  • Extend native libraries
  • Replace native libraries altogether

29. What are some naming conventions you should follow while creating libraries?

There are 3 conventions you should follow to make your own life easier:

  • Capitalize all file names, e.g. Sampleclass.php
  • Capitalize all class declarations, e.g. class Sampleclass
  • Choose the same name for your files and classes

30. What is the basic prototype of a class file?

Here is the basic prototype of a class file:

<?php

defined(‘BASEPATH’) OR exit(‘No direct script access allowed’);

class Someclass {

public function some_method() { }

}

31. What is the method to extend a class in CodeIgniter?

To extend a class, you have to create a file named MY_Input.php in the application/core/ directory with the declaration:

Class MY_Input extends CI_Input { }

Note that once again, the name of the class and file has been kept consistent.

32. What is an example of a URL routing different from the default one?

By default, CodeIgniter follows a standard structure for the URL which gives the first segment to the class and the second to the function. This is what it looks like:

something.com/class/function/id

However, you can choose to have a different structure. Here is one example:

something.com/product/1

something.com/product/2

something.com/product/3

In this example, the second segment is kept for a product ID instead of a method.

33. How do you specify your own routing rules?

First, you have to navigate to application/config/routes.php, and locate the array called $route. Next, you can use either a wildcard entry or a regular expression.

34. What are wildcards and regular expressions?

You can have two kinds of wildcards:

  • Matching only numbers through: num-series
  • Matching only characters through: any-series

Here is an example of creating a regular expression to route through your own URL choice:

$route[‘blog’(a-zA-Z0-9]+)’] = ‘women/social’;

35. What is the purpose of configuring URL routes?

There are several reasons why you would configure URL routes:

  • It can increase the page visits to your website through better search engine visibility
  • It can help hide the complex expressions from your users

36. What are hooks in CodeIgniter?

Hooks are features that allow you to manipulate the way CodeIgniter normally executes its program, without going into the relevant files and meddling with the code.

This is particularly useful if you want to change the order of execution of scripts or add in a controller call before the view is loaded.

37. How do you enable hooks in CodeIgniter?

Navigate to the application/config/config.php file and switch the following setting to ‘TRUE’ as shown below:

$config[‘enable_hooks’] = TRUE;

38. What is the method to define a hook in CodeIgniter?

You can define hooks by going to the application/config/hooks.php file and using this prototype:

$hook[‘pre-controller’] = array(

‘class’ => ‘MyClass’,

‘function’ => ‘Myfunction’,

filename’ => ‘Myclass.php’,

        ‘filepath’ => ‘hooks’,

       ‘params’   => array(‘beer’, ‘wine’, ‘snacks’) );

39. What are drivers in CodeIgniter?

Drivers are a different kind of library in CodeIgniter that have a structure involving a parent class and multiple child classes, each of which can access the parent class but not the sibling classes.

Drivers provide clean and crisp syntax for you to implement discreet classes in your own application.

40. How can you make use of a driver in CodeIgniter?

You have to initialize it inside the controller using the following line of code:

$this -> load -> driver(‘class_name’);

41. How can you create a driver in CodeIgniter?

You have to take care of 3 things if you want to make a driver.

  • Create a file name such as ‘Driver_name.php’
  • Create a list of drivers inside this file
  • Write the code for each individual driver

Here is how you could connect multiple databases at the same time (simultaneously):

$db1 = $this -> load -> database(‘group_one’, TRUE);

$db1 = $this -> load -> database(‘group_two’, TRUE);

43. What are security methods in CodeIgniter?

CodeIgniter has several ways of implementing security in your application and protecting the raw input data fed to your program. Here are 3 methods:

  • CSRF (Cross-site Request Forgery)
  • Class reference
  • XSS filtering

44) Why is there a need to configure the URL routes?

Changing the URL routes has some benefits like

  • 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. from the users for security reasons
  • Provide different functionality to particular parts of a system

45) List out different types of hook point in CodeIgniter?

Different types of hook point in Codeigniter includes

  • post_controller_constructor
  • pre_controller
  • post_system
  • pre_system
  • cache_override
  • display_override
  • post_controller

46) Mention what are the security parameter for XSS in CodeIgniter?

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.

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

48) Explain what is inhibitor in CodeIgniter?

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.

49) 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. Codeigniter can be accessed using the URL helper. For example http://servername/controllerName/controllerFunction/parameter1/parameter2.

50) Explain how you can extend the class in CodeIgniter?

To extend the native input class in CodeIgniter, you have to build a file named application/core/MY_Input.php and declare your class with

Class MY_Input extends CI_Input {

}

13) Explain how you can prevent CodeIgniter from CSRF?

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.data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==

laravel interview questions
codeigniter interview questions for freshers
codeigniter interview questions for 3 years experience
codeigniter interview questions 2020
php codeigniter interview questions and answers
codeigniter interview questions advanced
codeigniter interview questions edureka
php interview questions for 5 years experience

Additional Reading

READ MORE

Leave a Comment

Your email address will not be published. Required fields are marked *