InterviewSolution
This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.
| 1. |
How can I see raw query generate by CI’s active record |
|
Answer» CodeIgniter is PHP opensource FRAMEWORK, based on loosely coupled MVC pattern. It consists of HUGE libraries, simple interface and logical structure to access these libraries, plugins, and helpers to solve complex logical problems. It’s a lightweight elegant framework which provides faster development RATHER than writing code from scratch. The latest version (as of now) is 3.1.10. It’s appropriate in the following situations to create a project with CodeIgniter.
|
|
| 2. |
I want to use the $config variable in my view files. What are the ways to use the same? |
|
Answer» Active RECORD is a database design pattern, CI uses a modified version of it. The pattern allows data to be inserted, updated or deleted in the database with nominal scripting. The MAJOR benefit to using the Active Record features is that it allows you to create database INDEPENDENT applications since the query syntax is generated by each database adapter. To get the raw query we can use $this->db->get_compiled_select(). Difference between get_compiled_select() and last_query() is that get_compiled_select GIVES the query STRING generated even if we don’t run the query against database. |
|
| 3. |
A PHP Error was encountered Severity: Notice Message: Only variable references should be returned by reference Filename: core/Common.php Line Number: 257 |
|
Answer» To USE the config into the view FOLLOWING WAYS can be USED.
|
|
| 4. |
I upgraded my server’s PHP to 5.6.20. After that, this is the following error I get. What should be done in order to resolve it? |
|
Answer» If your CODEIGNITER version is prior to 2.2, then one can get this error. The ideal solution is to upgrade CI to LATEST. If that’s not possible then edit the core/Common.php file and make the following change: $_config[0] = & $config; RETURN $_config[0]; |
|
| 5. |
Explain session in codeIgnitor. |
|
Answer» Sessions allows us to MAINTAIN the user “states” and helps to track user activity. In codeIgnitor session can be loaded with $this->load->library(‘session’); post LOADING session library object we can use with $this->session. To READ the session data : $this->session->USERDATA(‘key’). Session data can also be get by $this->session->item To create a session in codeIgnitor: Session’s Class set_userdata() method is used to create a session in CodeIgniter. This method takes an associative array containing your data that you want to add in session. To add userdata one value at a time, set_userdata() also can be use with syntax: $this->session->set_userdata('some_name', 'some_value');To REMOVE session data : It can be done with unset a particular key $this->session->unset_userdata('some_key');Unset array of item keys : $array_items = array('username', 'email'); $this->session->unset_userdata($array_items); |
|
| 6. |
What are the available hooks in codeIgnitor. |
|
Answer» Hooks are the events which can be called before or after the execution of a program. Hooks stored in a specific DIRECTORY allows the codeIgnitor execution PROCESS without MODIFYING core FILES. There are two hook files in CodeIgniter. One is application/config/hooks.php folder and other is application /hooks folder.
To define a hook : $hook['post_controller'] = array( 'class' => 'Classname', 'function' => 'functionname', 'filename' => 'filename.php', 'filepath' => 'hooks', 'params' => array('element1', 'element2', 'element3') );
List of available hooks are :
|
|
| 7. |
Explain Inhibitor and error handling in CodeIgnitor. |
|
Answer» LIKE every other framework, codeIgnitor let you build the error reporting, also error logging class permits errors and debugging messages to be saved as a text FILE. Following are the common error logging functions:
To enable error logging you MUST set the “threshold” for logging in application/config/config.php. Also, your logs directory must be writable. $config['log_threshold'] = 1; 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 errors, exceptions, and fatal errors. |
|
| 8. |
Explain controllers in codeIgnitor. |
|
Answer» Controller is an intermediary between MODELS and views. It PROCESSES HTTP requests and generates an HTML response. It’s the central part of every REQUEST coming to application. knowledgehut.com/interview-questions/codeignitor Codeintor will try to find Interview-questions.PHP file and InterviewQuestions Class. Basic syntax of controller is like : <?php defined(‘BASEPATH’) or exit(‘Direct script execution not allowed’’); Class First extends CI_Controller { Public function index() { //code goes here }Please note that FILENAME here would be First.php and classname will be First. Default controller for Codeigniter is Welcome.php which we can see post installation. |
|
| 9. |
Explain Model in codeIgnitor and how to load them. |
|
Answer» In any dynamic application we need to call some function to get data from the database. MODEL handles all data logic and representation and loads the data to view. <?php Class FirstModel extends CI_Model { function __Construct() { parent::_construct(); } }FirstModel is the model NAME and same should be your file name. It extends the core codeIgnitor model which provides all the built-in functionality. To load a model use : $this->load->model('ModelName'); Manually connect database by adding $this->load->database(); in the page whereever required. Connect model to database : .Auto-connect feature helps to load database with every page load. To enable it add ‘database’ to array library autoload .php It will be LIKE : $autoload[‘LIBRARIES’] = array(‘database’); |
|
| 10. |
What is a helper in codeIgnitor. |
|
Answer» Helper in codeIgnitor helps you to ACCOMPLISH different TASKS. Each helper file is a collection of functions which are aimed to solve a particular problem. Helpers are not written in an object-oriented way rather in a procedural way, independent of each other. Helpers can be of file helper, text helper, URL helper, form helper etc.
To use the helpers we need to load it. CodeIgnitor will first look into application/helpers directory for the same and if it’s not found there then will look into system/helpers directory. |
|
| 11. |
Explain libraries in codeIgnitor and how to extend the native library. |
|
Answer» Libraries are a feature-rich set of code for a particular functionality. CODEIGNITOR provides a rich set of libraries which increases the development speed of application. Default libraries can be located at system/library directory. To Load single library please use $this->load->library(‘class_name’); To Load multiple libraries please use $this->load->library(array(‘class_name1’, ‘class_name2’, ‘class_name3’)); Custom libraries to be created in the application/libraries directory. If we want to add one or more METHODS to the existing library then it will replace the entire library with a modified version. EXTENDING the library is a BETTER option. There are rules to do the same.
|
|
| 12. |
What is hooks in codeIgnitor and explain some of them with example. |
|
Answer» Hooks provides a way in codeIgnitor to modify the INNER working of a framework WITHOUT hacking the core files of the framework. Codeingitor follows a specific process to execute REQUEST (explained in QA3) but there may be some instances when a developer wants to trick it. Hooks plays a vital role to deal with such situations. The hooks feature can be globally enabled/disabled by setting the $config['enable_hooks'] = TRUE; In the application/config/config.php file: Hooks can be defined in the application/config/hooks.php file. Each hook is specified as an array in the following format. $hook['pre_controller'] = array( 'class' => 'MyClass', 'function' => 'Myfunction', 'filename' => 'Myclass.php', 'filepath' => 'hooks', 'params' => array(param1, ‘PARAM2’, 'param3') );Different types of hook POINTS in codeIgnitor are:
|
|
| 13. |
Default URL pattern and how to make changes in the same. |
|
Answer» The default URL structure for CodeIgniter is SEO and human-friendly. Codeignitor uses SEGMENT based approach over to query string. knowledgehut.com/interview-questions/codeignitor The SEGMENTS in the URL with the Model-View-CONTROLLER approach, usually represent: knowledgehut.com/class/function/ID. Here Class which is our first segment REPRESENTS controller class. Function Our 2nd segment represents which function or method of the controller to be called. ID Our 3rd segment represent that any additional ID or Parameter to be pass to the controller. To access the URL pattern we need to use URI Library or URL Helpers available in codeignitor. By Default, Index.php is part of your URLs. Like knowledgehut.com/index.php/interview-questions/codeignitor To remove index.php, if your apache has mod-rewrite enabled then modify the .htaccess file with SIMPLE rules. RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php/$1 [L] |
|
| 14. |
Explain MVC in codeIgnitor. |
|
Answer» CodeIgnitor is based on MVC. Acronym for Model View Control. MVC separates application logic from presentation logic. As the presentation is separated from PHP logic so page CONTAINS less scripting and provides FASTER loading. Model REPRESENTS data structures Model CLASSES will contain functions that help to retrieve update and insert information into the database. Controller holds all application logic, serves as an intermediary between model and view and any other resources NEEDED to process request. View is the information that is been passed to the user. View is normally a web page but in CodeIgnitor it can be a part of a page like a header, footer, nav, etc. CodeIgnitor holds a loosely MVC approach. If your application doesn’t require a model, you can ignore them and can build an application with View and controllers. |
|
| 15. |
What is a helper in CodeIgnitor and how it can be loaded. |
|
Answer» Helper as the name suggests functions which aid to your program. Helper is simply a collection of functions in a PARTICULAR category. There are VARIOUS types of helpers like URL helpers, Form helpers, Text helpers, COOKIES helper, FILE helpers, etc. Helpers are independent procedural functions. A helper can be loaded in controller constructor which makes them available globally. A helper can be loaded like: $this->load->helper(‘file_name’); // It will load file helper. To load URL in helper use URL. $this->load->helper(‘url’);To load multiple helpers : $this->load->helper->array(‘first_helper’,’second_helper’,’third_helper’); |
|
| 16. |
Explain the architecture / Application flow of Codeignitor. |
|
Answer» Source Index.php FILES act as a front controller so requests first come here. It initialises base resources required to run Codeignitor. Now Router determines what should be done with requests. If a cache file EXISTS for the request then information is directly passed to the browser and further process is ignored. If the cache doesn’t exist then before loading the APPLICATION controller, HTTP requests PASS through the security check. Index.php -> Route -> Security. Once the security check is done for submitted data, the request is passed to the Application controller. Application Controller loads all the necessary files like controller, model, view, libraries, HELPERS, etc and processes the request. Index.php -> Route -> Security -> Application Controller -> Models, Library Now processed data from application controller has been sent to View. The view will provide the page with available data for caching so that further requests can be handled faster. |
|
| 17. |
Explain directory structure of codeIgnitor. |
|
Answer» CodeIgnitor directory structure consists of mainly 3 parts.
Application is the main folder for developers where they can find all project files. It consists of models, views, controllers, configuration files, etc. All the project code, a DEVELOPER works with will be in this directory. Main subdirectories of Application are cache, config, CONTROLLER, core, helper, hooks, language, libraries, LOGS, model, views and third_party. Main subdirectories of System are core, DATABASE, fonts, helpers, language, libraries. Inside users-guide, subdirectories which can be found are cilexer and source. It provides complete documentation of FUNCTIONS, libraries, and helpers of CodeIgnitor. A good resource if you stuck or don’t have much knowledge then follow users_guide. |
|
| 18. |
What is CodeIgniter and what are its features? |
|
Answer» CodeIgniter is PHP opensource framework, based on loosely coupled MVC pattern. It consists of huge libraries, simple interface and logical structure to access these libraries, plugins, and helpers to solve complex logical PROBLEMS. It’s a lightweight elegant framework which provides faster development rather than writing code from scratch. The latest VERSION (as of now) is 3.1.10. It’s appropriate in the following situations to CREATE a project with CodeIgniter.
|
|