CodeIgniter

CodeIgniter - Interview Questions and Answers

CodeIgniter- the best interview questions and answers


CodeIgniter - Interview Questions and Answers

codeigniter logo

  1. Start with the basics: What is CodeIgniter?
    Codeigniter is an open source framework for web application. It is used to develop websites on PHP. It is loosely based on MVC pattern, and it is easy to use compare to other PHP framework.
     
  2. Why is CodeIgniter a smart framework to use - give me 3 reasons?
    • ​Small footprint with exceptional performance
    • MVC approach to development (although it is very loosely based which allows for flexibility)
    • Generates search engine friendly clean URLs
    • Easily extensible
    • Runs on both PHP 4 (4.3.2+) and 5
    • Support for most major databases including MySQL (4.1+), MySQLi, MS SQL, Postgres, Oracle, SQLite,
      and ODBC.
    • Application security is a focus
    • Easy caching operations
    • Many libraries and helpers to help you with complex operations such as email, image manipulation,
      form validation, file uploading, sessions, multilingual apps and creating apis for your app
    • Most libraries are only loaded when needed which cuts back on resources needed
       
  3. Explain what are hooks in CodeIgniter?
    Codeigniter’s hooks feature provides a way to change the inner working of the framework without hacking the core files. In other word, hooks allow you to execute a script with a particular path within the Codeigniter.  Usually, it is defined in application/config/hooks.php file.
     
  4. Explain how you will load or add a model in CodeIgniter?
    Within your controller functions, models will typically be loaded; you will use the function
    $this->load->model (‘Model_Name’);
  5. Explain what helpers in CodeIgniter are and how you can load a helper file?
    In CodeIgniter, helpers are group of function in a particular category that assist you to perform specific functions. In CodeIgniter, you will find many helpers like URL helpers- helping in creating links, Text helpers- perform various text formatting routines, Cookies- helpers set and read cookies.  You can load helper file by using command $this->load->helper (‘name’) ;

    codeigniter flow

 

 

 

 

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

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

  3. Explain Application Flow Chart in CodeIgniter?

    The following graphic illustrates how data flows throughout the system:
    (Give the following image explain the flow of CodeIgniter)

    • The index.php serves as the front controller, initializing the base resources needed to run CodeIgniter.

    • The Router examines the HTTP request to determine what should be done with it.

    • If a cache file exists, it is sent directly to the browser, bypassing the normal system execution.

    • Security. Before the application controller is loaded, the HTTP request and any user submitted data is filtered for security.

    • The Controller loads the model, core libraries, helpers, and any other resources needed to process the specific request.

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

  4. How to access config variable in codeigniter?
    You can access config variable like:
     $this->config->item('variable name');

  5. How to unset session in codeigniter?
    We can use unsetuserdata to destroy particular session variable
    $this->session->unset_userdata('somename');
    We can use sessdestroy to destroy all session:
    $this->session->sess_destroy();

  6. How do you use aliases with autoloading models in CodeIgniter?
    We can auto load model like this:
    $autoload['model'] = array(array('usersmodel', 'users'), array('newsmodel', 'news'), 'categorymodel');

  7. List out different types of hook point in CodeIgniter?
    Different types of hook point in Codeigniter includes

    1. post_controller_constructor

    2. pre_controller

    3. post_sytem

    4. pre_system

    5. cache_override

    6. display_override

    7. post_controller

  8. 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.
  9. Explain how you can link images/CSS/JavaScript from a view in CodeIgniter?
    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
    /css/styles.css
    /js/query.php
    /img/news/566.gpg
  10. 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.
  11. 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.​
  12. 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 OR XSS?​
    There are several ways to protect CodeIgniter from CSRF/XSS ( cross-site scripting), 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.
  14.  How to print SQL statement in codeigniter model?​
    $this->db->last_query();
  15. How do you get last insert id in codeigniter ?
    $this->db->insert_id();​

Published: 1st June 2016 by

Adverts