CodeIgniter is a PHP open-source web development framework for building dynamic websites. Based on the MVC development pattern, Codeigniter can be modified to use the HMVC pattern. This pattern allows developers to maintain controller, view, and models in a sub-directory format. This framework is known for its higher speed as compared to other PHP frameworks. If you are looking for Codeigniter interview questions, we can help you!
A Quick Overview of CodeIgniter | |
---|---|
What is Codeigniter? | It is a PHP open-source web development framework for building dynamic websites. Based on the MVC development pattern, Codeigniter can be modified to use the HMVC pattern. |
Latest Version | 4.0, released on September 3, 2019 |
Stable Version | 3.1.10, released on January 16, 2019 |
Created By | EllisLab |
Written in | PHP |
License | MIT License |
Here in this article, we will be listing frequently asked CodeIgniter Interview Questions and Answers with the belief that they will be helpful for you to gain higher marks. Also, to let you know that this article has been written under the guidance of industry professionals and covered all the current competencies.
It is a PHP open-source web development framework which is used for building dynamic websites.
4.0 is the latest version of the Codelgniter framework. This version is released on September 3, 2019.
Here are some new features that support Codelgniter
Codelgniter updated these modules to enhance features.
Currently Codeigniter supports these databases that's are given below :-
It is a type of events which can be called before and after the execution of a program in CodeIgniter. For example Hooks can be used where we need to check whether a user is logged in or logged out before the execution of controller.
It can be globally enabled or disabled by setting in the application/config/config.php
page
It can be defined in application/config/hooks.php
page. In CodeIgniter each hook is specified like an array with this prototype:
You can do it with this $this->load->model("ModelName");
You can use this $this->load->view('name');
to load a view in CodeIgniter.
In CodeIgniter when a specified file in the default controller loaded by default when there is no file name mentioned in URL. By default "welcome.php" file is loaded after installing CodeIgniter.
Yes, we can change it from application/config/routes.php
. We have to pass our controller name which we want to load by default in $route['default_controller']
For example, now the frontend is the default controller $route['default_controller'] = 'Frontend';
from now. Interview questions on Codeigniter are always a level up and thus a little tough to crack.
The basic URL structure is bestinterviewquestion.com/class/function/ID
.
Here class represents controller's class that needs to be invoked.
Here "function" is name of method which is called.
ID is an additional parameter that is passed to controllers.
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.
How we can load a Helper : $this->load->helper('name');
Here "name" is file name of helper.
How to load multiple Helpers at one time.
$this->load->helper(array('helper1', 'helper2', 'helper3'));
// Here helper1,helper2,helper3 are different helper's name.
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
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.
It is the process of taking a URI endpoint and decomposing it into parameters to determine module controller and action of that controller should receive the request.
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
.
Codeigniter supports various hook points.
If you are scrolling on the web for Codeigniter interview questions for 2 year experience pdf, then this guide will gonna help you.
We can use an absolute path to resources to link images/CSS/JavaScript
from a view in CodeIgniter.
For Example :
<link rel = "stylesheet" type = "text/css" href = "<?php echo base_url(); ?>css/responsive.css">
<script type = 'text/javascript' src = "<?php echo base_url(); ?>js/jquery.js"></script>
Inhibitor is an error handling class. It is using the native PHP functions like set_error_handler, set_exception_handler, register_shutdown_function to handle all error's like parse errors, exceptions, and fatal errors.
You can follow these given steps to remove index.php from URL in Codeigniter.
config.php
and change from $config['index_page'] = "index.php"
to $config['index_page'] = "";
$config['uri_protocol'] ="AUTO";
to $config['uri_protocol'] = "REQUEST_URI";
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
In codeIgniter CSRF token is a random generated value that changes with each HTTP request sent by webform. When the form is submitted by user then website checks this submitted CSRF token equals or not the saved in the session.
We can enable CSRF protection from config.php
and update the following values
$config['csrf_protection'] = TRUE;
$config['csrf_token_name'] = 'csrf_test_name';
//The token name
$config['csrf_cookie_name'] = 'csrf_cookie_name';
//The cookie name
$config['csrf_expire'] = 7200;
// It will expire after this given time.
You have to build a file with name application/core/Blog.php
and declare your class with
Class Blog extends CI_Input {
// Write your code here
}
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'));
$_SESSION['username'] = 'bestinterviewquestion.com'
// It can be use in core PHP
$this->session->set_userdata('username', 'bestinterviewquestion.com');
// It can be done in Codeigniter
We can pass array to store values in session in Codeigniter.
$array = array(
'username' => 'bestinterviewquestion.com',
'email' => '[email protected]',
'url' => 'https://www.bestinterviewquestion.com'
);
$this->session->set_userdata($array);
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.
$this->session->userdata('username');
// Here we will get username, if it is exists in session.
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.
CodeIgniter can be installed with four steps that are given below:
application/config/config.php
file and set base URL.application/config/database.php
file and set your database credientails.If you want to configure additional setting then you can click here
We need only PHP version 5.6 or greater than 5.6. because of potential security and performance issues.
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')
);
If you want to enable hooks then you have to enabled/disabled by setting in the application/config/config.php
file.
If you want to enable hooks then use this $config['enable_hooks'] = TRUE;
In CodeIgniter drivers are a very special type of Library that has a parent class and its child classes. Child classes have the access to its parent class but not their siblings. It provides an elegant syntax in your controllers file for libraries.
It can found in the system/libraries/ directory.
If we want to use a driver we have to initialize it within a controller with this given method.
$this->load->driver('class_name');
Here class_name is the name of the driver class which we want to invoke.
/application/libraries/
/application/libraries/Your_Driver
/application/libraries/Your_Driver/drivers
You can use $this->db->last_query();
to display the query string.
You can use print_r($query);
to display the query result.
You can create your custom 404 page with these steps. Please follow these steps one by one.
$route['404_override'] = 'Controller_Class_Name';
. You have to update this in your application/config/routes.php
Controller_Class_Name
" file in your controllers directory application/controllers/
.You can use $this->db->insert();
to insert data in your database in Codeigniter.
If you want to insert this in your "Admin Table".
$dataArray = array(
'name'=>'bestinterviewquestion.com',
'phone'=>'9971083635',
'email'=>'[email protected]'
);
$this->db->insert('Admin',$dataArray);// Here "Admin" is My TableName
You can use $lastInsertID = $this->db->insert_id();
after insert query.
$this->db->join('blog', 'comment.blog_id= blog.id', 'inner');
OR
$this->db->join('blog', 'comment.blog_id= blog.id');
To set config value $this->config->set_item('mainURL','bestinterviewquestion.com');
we can use this.
To get config value $this->config->set_item('mainURL');
we can use this.
You can use $this->input->ip_address();
this to get IP Address.
You can use this $this->router->fetch_class();
to get current controller name.
You can use this $this->router->fetch_method();
to get current method.
You can use $this->db->delete('admin', array('id' => 5));
this query to delete record according to condition
// DELETE FROM admin WHERE id = 5
// TRUNCATE table admin;
You can use this to truncate table
$this->db->from('admin'); // select admin table
$this->db->truncate(); // truncate admin table
OR
You can also use like this
$this->db->truncate('admin'); // truncate admin table
Step 1. Make /application/logs
log folder writable
Step 2. Set $config['log_threshold'] = 1;
in /application/config/config.php
We can use the redirect() function to redirect on any page.
$this->load->helper('url');
// Firstly you have to load url helper.
if (condition != TRUE) {
redirect('login_page');
}
$database1 = $this->load->database('DB1', TRUE);
// Connect with database 1
$database2 = $this->load->database('DB2', TRUE);
// Connect with database 2