Home >>Interview Questions >CodeIgniter Interview Questions
Codeigniter based on the MVC pattern is an open sourced framework which develops websites for PHP.
Hooks are a feature in codeigniter which help the user to change the inner working without having to change the core files. They are represented by application/config/hooks.php.file. Hooks are more often to be used in executing a script with a particular path within codeigniter.
Helpers is the feature in codeigniter where it assists the user in performing specific function. The helper will vary according to the function it’d be assisting such as url helpers will assist in creating links whereas text helpers assist in performing text formatting routines.
To load a file in codeigniter you use
Routing is defined as serving PHP files in an alternative way rather than accessing it from the browser. This allows the user to gain unlimited freedom in customizing the default URL pattern to fit his own requirement.
To prevent hacking and other malicious activities codigniter applies a cross site scripting hack filter which will automatically start to filter all post and cookie data while comparing it to existing methods that are used to trigger javascript and once an anomaly is detected it is automatically converted into character entries.
To link images from a view in a codeigniter first use an absolute path to the resources required and link the image from a view in codeigniter /css/styles.css /js/query.php /img/news/566.gpg
Inhibitor is a specialist class in codeigniter which primarily functions as a means to combat or handle errors and is primarily comprised of functions such as set_exception_handler set_error_handler register_shutdown_function which are useful in handling parse errors exceptions and fatal errors.
The default URL patter in codeigniter is split up into four functions which start with the server name,controller class controller name and finally function parameters To accesses the URL pattern in codeigniter you have to use a URL helper.
To extend a native input class the user must first build a file which is to be named application/core/MY_Input.php And then proceed to declare the class with Class MY_Input extends CI_Input { }
The most common way to protect codeigniter form CRSF is to use a hidden field in every form of the website. This hidden field will be used as a CRSF token which in a random value which changes each and every HTTP request sent. When inserted into a website form it is saved in the user session if the request is the same it becomes legit.
To enable CRSF the user must follow these steps application/config/config.php file and setting it to $config [ ‘csrf_protection’] = TRUE;
There are many features that are prominent and are availed to us by the Codeigniter.
Here is a list of the most important features of the Codeigniter depicted below:
MVC is the pattern on which the whole of codeIgniter framework is based. Now let’s get to know the MVC, it is basically a software that delivers the users a separate logical view from the presentation view.
This results in a web page that contains the minimal scripting.
Model in the CodeIgniter is generally responsible to handle all the data logic and the representation and loading of the data that are present in the views. The model in the CodeIgniter is generally stored in the application/models folder.
Here is the basic structure of a model file is depicted below for you to understand:
class ModelName extends CI_Model { public function MethodName() { } }
In the above mentioned structure, the ‘ModelName’ is representing the name of the programmer’s model file. Please note that the class first letter should be in an uppercase letter that should be followed by the various other lowercase letters, and it should be generally the same as the name of the file of the programmer. All the built-in methods of parent Model file basically gets inherited to the newly created file as the base CodeIgniter Model is extended by the model name.
In order to connect the models to a database manually the following depicted syntax is used:
$this->load->database();
All the markup files like header, footer, sidebar, etc are present in the view folder. In order to reuse them the programmer has to embed them in the controller file anywhere. This is to be done as they can't be called directly hence, they have to be loaded in the controller's file.
Syntax of the view:In order to view the syntax the programmers have to create a file and have to save that in the application/views folder. For a better understanding please review the following example:
<!DOCTYPE html> <html> <head> <title>Codeigniter View</title> </head> <body> <h1>Codeigntier View</h1> </body> </html>
In the Codeigniter, the View can't be accessed directly as of the fact that it is loaded in the controller file for always. Here is the function that is depicted below is used to load a view page:
$this->load->view('page_name');
The intermediary present between the models and the views to process the HTTP request and is used to generate a web page is known as a controller. It is basically known as the center of each request that exists on the web application of the user.
Please consider the following URL in this reference,
projectName/index.php/welcome/
In this URL the CodeIgniteris basically trying to find the welcome.php file and the Welcome class.
Controller Syntaxclass ControllerName extends CI_Controller { public function __construct() { parent::__construct(); } public function MethodName() { } }
Whenever there is no file name is mentioned in the URL then the file will be specified in the default controller loaded by default. By default, the file name is welcome.php that is known as the first page that is to be seen after installing CodeIgniter.
localhost/codeigniter/
In this case the Welcome.php will generally be loaded as if there is no file name mentioned in the URL provided.
The programmer can generally change the default controller that is present in the file application/config/routes.php as per his/her need.
$route['default_controller'] = ' ';
In the above syntax the programmer has to specify the file name that he/she basically wants to get loaded as the default.
In order to use a constructor, the programmer has the need to mention the below depicted line of code,
parent::__construct()
The basic CodeIgniter URL structure generally uses a segment based approach instead of using the 'query-string' approach.
The basic CodeIgniter URL structure is as depicted below:
projectName/index.php/class/method/ID
A controller class that generally needs to be invoked is represented by the Class.
ID is basically an additional segment that is passed to the controllers.
Inhibitor is generally known to be an error handler class in the CodeIgniter that basically uses the native PHP functions such as set_error_handler, set_exception_handler, register_shutdown_function in order to handle the parse errors, exceptions, and the fatal errors.
In order to load the multiple helper files the programmer generally have to specify them in an array just like depicted below:
$this->load->helper(array('helper1', 'helper2', 'helper3'));
A rich set of libraries is generally availed by the CodeIgniter. As it generally enhances the developing speed of an application hence, it is known as the essential part of the CodeIgniter. The normal location of this in the CodeIgniter is the system/library.
Here is how a programmer can load it, just by following the code depicted below:
$this->load->library('class_name');
There are generally three methods that can be used to create a library that are depicted below:
A library that is newly created in CodeIgniter structure should generally be placed in application/libraries folder.
The answer is indeed, a programmer can add some of the extended functionality to a native library just by adding one or two methods in it. These methods will replace the entire library with the programmer’s version. Hence, it is always better to extend the class. Please note that the extending and replacing in codeIgniter is almost known to be identical with only exceptions that are depicted below: