CodeIgniter Interview Questions

CodeIgniter Interview Questions Download CodeIgniter Interview Questions PDF

CodeIgniter Interview Questions

In Codeigniter, 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 model’s directory. For example, if you have a model located at application/models/blog/Posts.php you’ll load it using:

$this->load->model('blog/Posts');

Once your Model loaded, you will access its methods using an object with the same name as your controller:

Example

class Blog_controller extends CI_Controller {
        public function blog()
        {
                $this->load->model('blog');

                $data['query'] = $this->blog->get_last_ten_entries();

                $this->load->view('blog', $data);
        }
}

Code for Checking a field or column exists or not in a Codeigniter table.

if ($this->db->field_exists('field_name', 'table_name'))
{
        // some code...
}

$this->load->library(‘library_name’); method is used to load a library in CodeIgniter.

Usage:
//Loading Cart library

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

Using Cart library methods

$data = array(
        'id'      => 'sku_9788C',
        'qty'     => 1,
        'price'   => 35.95,
        'name'    => 'T-Shirt',
        'options' => array('Size' => 'L', 'Color' => 'Red')
);

$this->cart->insert($data);

CodeIgniter DB Class insert_id() method is used to get last insert id.

Usage:

function add_post($post_data){
   $this->db->insert('posts', $post_data);
   $insert_id = $this->db->insert_id();
   return  $insert_id;
}

Csrf is used to set the protection in CodeIgniter. To set csrf, you have to put the corresponding config value True.

Syntax: $config['csrf_protection'] = TRUE;

In system/core/CodeIgniter.php, check CI_VERSION constant value define(‘CI_VERSION’, ‘3.0.6’);

In Codeigniter is_cli() method is used to check request is from the command line or not.
Returns TRUE if the application is run through the command line and FALSE if not.

As on Sept 3, 2018 CodeIgniter 3.1.9 is the latest version of the framework. You can download it from here

order_by function is used to order the records from a table in CodeIgniter.
// Getting random rows from database in CodeIgniter
$this->db->select('*');
$this->db->from('table_name');
$this->db->order_by("column_name", "random");
$result = $this->db->get()->result(); 

An inhibitor is an error handling class in Codeigniter. It uses PHP ‘s native functions like register_shutdown_function , set_exception_handler, set_error_handler to handle parse errorsexceptions, and fatal errors.

Object-relational mapping (ORM) is programming technique for converting data between incompatible type systems using object-oriented programming languages. Below is the list of ORM’s supported by CodeIgniter Framework
  • DataMapper
  • Doctrine
  • Gas ORM

Below images give you an understanding of how data flows throughout the system in Codeigniter.

Codeigniter Application Flow Chart

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

Source: https://www.codeigniter.com/user_guide/overview/appflow.html

CodeIgniter is a powerful MVC based PHP framework with a very small footprint, built for developers who need a simple and elegant toolkit to create full-featured web applications.

In Software engineering routing is the process of taking a URI endpoint (that part of the URI which comes after the base URL) and decomposing it into parameters to determine which module, controller, and action of that controller should receive the request.In Codeigniter typically there is a one-to-one relationship between a URL string and its corresponding controller class/method.The segments in a URI normally follow this pattern:example.com/class/function/id/.In CodeIgniter, all routing rules are defined in your application/config/routes.php file.