Loading…

An Overview of CodeIgniter

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 HMVC pattern. This pattern which 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!

Development History of CodeIgniter

The first version was released in Feb 2006 by EllisLab.

Latest Version: 3.1.9 is the newest version released in Dec 2018.

For more details, you can visit its official website Click here to go there

Advantages

If you are preparing for CodeIgniter Advanced Interview Questions, do memorize the powers of CodeIgniter.

  • Easy to learn, handle and customize
  • Flexible and easy to configure
  • Amazing Active Record Implementation
  • Best-in-class documentation
  • Discount, Offers, Coupons and Options and Properties
  • Extensive user base

Here are some of the “CodeIgniter Questions and Answers” for freshers as well as experienced candidates:

Last update: 24 Mar 2019, 40 Questions and Answers

CodeIgniter Interview Questions and answers

Looking for a new job? Do not miss to read our CodeIgniter Interview Questions and answers. Whether you are a fresher or an experienced, these questions and answers that can help you to crack your interview.

It is a PHP open-source web development framework which is used for building dynamic websites.

16 1

3.1.9 is the latest version of the Codelgniter framework. This version is released in 12 June 2018.

Here are some new features that supports Codelgniter 3.1.9

  • URL Helper
  • Query builder
  • Email Library
  • Form Validation Library
  • Session Library

Codelgniter updated these modules to enhance features.

8 1

Currently Codeigniter supports these databases that's are given below :-

  • MySQL
  • Oracle
  • PostgreSQL
  • SQLite
  • ODBC
  • Firebird etc
8 0

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:

3 0

You can do it with this $this->load->model("ModelName");

3 0

You can use this $this->load->view('name'); to load a view in CodeIgniter.

2 0

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 frontend is default controller $route['default_controller'] = 'Frontend'; from now.

1 1

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.

2 0

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.

2 0

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

2 0

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.

1 1

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.

1 0

Codeigniter supports various hook point.

  • post_controller_constructor
  • pre_controller
  • post_sytem
  • pre_system
  • cache_override
  • display_override
  • post_controller
3 0

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.

3 0

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]
3 0

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.

1 0

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
}

2 0

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'));

2 0
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 Codeigniter

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

2 0

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.

2 0

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

If you want to configure additional setting then you can click here

1 0

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

2 0

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')
);

1 0

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;

0 0

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.

How we can create our own drivers in CodeIgniter.
  • You can create your own driver folder in /application/libraries/
  • Create your_Your_Driver_name.php file in /application/libraries/Your_Driver
  • In /application/libraries/Your_Driver/drivers
    • Your_Driver_name_subclass_1.php
    • Your_Driver_name_subclass_2.php
1 0

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

1 0

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

    class 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.
0 0

You can use $this->db->insert(); to insert data in your database in Codeigniter.

Example

                                                    

If you want to insert this in your "Admin Table".

$dataArray = array(
       'name'=>'bestinterviewquestion.com',
       'phone'=>'9971083635',
       'email'=>'info@bestinterviewquestion.com'
);

$this->db->insert('Admin',$dataArray);

// Here "Admin" is My TableName

1 0

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

0 0

$this->db->join('blog', 'comment.blog_id= blog.id', 'inner');

OR

$this->db->join('blog', 'comment.blog_id= blog.id');

0 1

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.

0 0

You can use $this->input->ip_address(); this to get IP Address.

1 0

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.

0 0

You can use $this->db->delete('admin', array('id' => 5)); this query to delete record according to condition
// DELETE FROM admin WHERE id = 5

0 0

// 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

1 0

Step 1. Make /application/logs log folder writable
Step 2. Set $config['log_threshold'] = 1; in /application/config/config.php

1 0

We can use the redirect() function to redirect on any page.

Example

                                                    

$this->load->helper('url');       // Firstly you have to load url helper.
if (condition != TRUE) {
    redirect('login_page');
}

0 1

$database1 = $this->load->database('DB1', TRUE); // Connect with database 1
$database2 = $this->load->database('DB2', TRUE); // Connect with database 2

1 0
Ask Question