
CodeIgniter | CodeIgniter Interview Questions | Basic
- Codeigniter is a light-weight, open source, php framework. Codeigniter is loosely based on MVC pattern.
- CodeIgniter is the most simplest framework in PHP, which is you will easily learn.
- It's mostly known for its speed as compared to other frameworks in php.
Its goal is to enable you to develop projects much faster than you could if you were writing code from scratch, by providing a rich set of libraries for commonly needed tasks, as well as a simple interface and logical structure to access these libraries.
CodeIgniter was created by EllisLab and is now a project of the British Columbia Institute of Technology. The first public version of CodeIgniter was released on February 28, 2006.
An Object Relational Mapper written in PHP for CodeIgniter. It is designed to map your Database tables into easy to work with objects, fully aware of the relationships between each other.
A lightweight and easy-to-use ORM for CodeIgniter. Gas was built specifically for CodeIgniter app. It uses CodeIgniter Database packages, a powerful DBAL which support numerous DB drivers. Gas ORM provide a set of methods that will map your database tables and its relationship, into accessible object.
NOTE: You must do some workaround to integrate this with CI, try here.
You may also like - Core PHP Interview Questions
- It is free to use as for open source framework.
- It is light weight. The core system requires only a few very small libraries. Not like other frameworks that require heavy file libraries.
- CodeIgniter is Fast. It is faster than any other framework in php.
- The URLs generated by CodeIgniter are clean and search engine friendly. You will change any url to what ever you want from files.
- CodeIgniter is Extensible. The system can be easily extended through the use of your own libraries, helpers, or through class extensions or system hooks.
- CodeIgniter Uses MVC (Model View Controller) which allows great separation between logic and presentation.
- CodeIgniter requires nearly zero configuration, does not require you to use the command line, not forced to learn a templating language.
- Full Featured database classes with support for several platforms, Security and XSS Filtering, Error Logging.
Since all three methods achieve the same ends. The question then is when do you use what?
Fortunately, CI has also provided that distinction in their user guide which you can go read it yourself.
For me, these are my guidelines on when to use what:- 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.
CodeIgniter is based on the Model-View-Controller development pattern. MVC is a software approach that separates application logic from presentation.
The Model represents your data structures. Typically your model classes will contain functions that help you retrieve, insert, and update information in your database.
The View is the information that is being presented to a user. A View will normally be a web page, but in CodeIgniter, a view can also be a page fragment like a header or footer. It can also be an RSS page, or any other type of "page".
The Controller serves as an intermediary between the Model, the View, and any other resources needed to process the HTTP request and generate a web page.
You may also like - CakePHP Interview Questions
CodeIgniter | CodeIgniter Interview Questions | Advanced
CodeIgniter’s Hooks feature provides a means to tap into and modify the inner workings of the framework without hacking the core files.How ever you like to cause some action to take place at a particular stage in the execution process.
The hooks feature can be globally enabled/disabled by setting the following item in the application/config/config.php file:
Hooks are defined in application/config/hooks.php file.
For example- post_controller_constructor
- pre_controller
- pre_sytem
- post_sytem
- cache_override
- display_override
- post_controller
You may also like - Top 50 Laravel Interview Questions
Helpers, as the name suggests, help you with tasks. Each helper file is simply a collection of functions in a particular category.
There are many inbuilt helpers few of them are:
- URL Helpers
- Form Helpers
- Text Helpers
- Cookie Helpers
- File Helpers
CodeIgniter does not load Helper Files by default, so the first step in using a Helper is to load it. Once loaded, it becomes globally available in your controller and views.
Helpers are typically stored in your system/helpers, or application/helpers directory.
CodeIgniter will look first in your application/helpers directory. If the directory does not exist or the specified helper is not located there CI will instead look in your global system/helpers folder.
Loading a helper file is quite simple using the following function:
Where helper_name is the file name of the helper, without the .php file extension or the "helper" part.
For example, to load the URL Helper file, which is named url_helper.php, you would do this:
A helper can be loaded anywhere within your controller functions (or even within your View files, although that's not a good practice), as long as you load it before you use it.
You can load your helpers in your controller constructor so that they become available automatically in any function, or you can load a helper in a specific function that needs it.
Note: The Helper loading function above does not return a value, so don't try to assign it to a variable. Just use it as shown.
Once you've loaded the Helper File containing the function you intend to use, you'll call it the way you would a standard PHP function.
For example, to create a link using the anchor() function in one of your view files you would do this:Where "Click Here" is the name of the link, and "blog/comments" is the URI to the controller/function you wish to link to.
To "extend" Helpers, create a file in your application/helpers/ folder with an identical name to the existing Helper, but prefixed with MY_ (this item is configurable. See below.).
For example, to extend the native Array Helper you'll create a file named application/helpers/MY_array_helper.php, and add or override functions:
All of the available libraries are located in your system/libraries folder.
In most cases, to use one of these classes involves initializing it within a controller using the following initialization function:
Where class name is the name of the class you want to invoke.
For example, to load the form validation class you would do this:
Once initialized you can use it as indicated in the user guide page corresponding to that class.
Additionally, multiple libraries can be loaded at the same time by passing an array of libraries to the load function.
You must supply one of three "levels" in the 1st parameter, indicating what type of message it is (debug, error, info), with the log message itself in the 2nd parameter.
- debug
- error
- info
- set_exception_handler
- set_error_handler
- register_shutdown_function
- register_shutdown_function
to handle parse errors, exceptions, and fatal errors.
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 the 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.
Open the routing file located at application/config/routes.php and add the following two lines.
- SEO-friendly URLs: 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.
- Security: from the users for security reasons Provide different functionality to particular parts of a system.
If you want to verify that a session value exists, simply check with has_userdata()
CodeIgniter supports "flashdata", or session data that will only be available for the next request, and is then automatically cleared.
This can be very useful, especially for one-time informational, error or status messages (for example: "N Record deleted").
However, if you want to be sure that you’re reading "flashdata" (and not any other kind), you can also use the flashdata() method:
If you want to mark multiple items as flashdata, simply pass the keys as an array:
If you find that you need to preserve a flashdata variable through an additional request, you can do so using the keep_flashdata() method. You can either pass a single item or an array of flashdata items to keep.
CodeIgniter also supports "tempdata", or session data with a specific expiration time. After the value expires, or the session expires or is deleted, the value is automatically removed.
To mark an existing item as "tempdata", simply pass its key and expiry time (in seconds!) to the mark_as_temp() method:
- If the expiration is omitted or set to 0, the default time-to-live value of 300 seconds (or 5 minutes) will be used.
- The userdata() method will NOT return flashdata items.
- The userdata() method will NOT return tempdata items.
To clear the current session (for example, during a logout), you may simply use either PHP’s session_destroy() function, or the sess_destroy() method. Both will work in exactly the same way:
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.
Go to the path - application/config/config.php
The CSRF token is added to the form as a hidden input only when the form_open() function is used.
A cookie with the CSRF token's value is created by the Security class, and regenerated if necessary for each request.
If $_POST data exists, the cookie is automatically validated by the Input class. If the posted token does not match the cookie's value, CI will show an error and fail to process the $_POST data.
So basically, it's all automatic - all you have to do is enable it in your $config['csrf_protection'] and use the form_open() function for your form.
CodeIgniter | CodeIgniter Interview Questions | Database
You may also like - MySQL Interview Questions
For example, if you have a model located at application/models/blog/Queries.php you'll load it using:
You may also like - RESTful Web Services Interview Questions