CodeIgnat Interview Question
CodeIgnat Interview Question
Questions
© Copyright by Interviewbit
Contents
Features of CodeIgniter:
Scope of CodeIgniter:
$hook[‘pre_controller’] = array(
‘class’ => ‘MyHookClass’,
‘function’ => ‘Myhookfunction’,
‘filename’ => ‘MyHookClass.php’,
‘filepath’ => ‘hooks’,
‘params’ => array(‘test’, ‘test1’, ‘webs’)
);
In the above code example, the ‘pre_controller‘ hook is called hook point. Various
types of hook points are available in CodeIgniter.
<?php
echo CI_VERSION;
?>
You can echo the constant value of CI_VERSION in the CodeIgniter controller or view
file.
The second method is to navigate to the system/core/CodeIgniter.php directory
which stores the current version number of CodeIgniter in a global constant
named ‘CI_VERSION’. Open the file and have a look at the lines:
/**
* CodeIgniter Version
*
* @var string
*
*/
define('CI_VERSION', '4.1.3');
Helper Library
All built-in helper file names are All built-in library files
suffixed with a word _helper do not have a specific
(ex: email_helper.php ). suffix.
Routing is a technique used in CodeIgniter, by which you can define your URLs
based on the requirement instead of using the predefined URLs. So, whenever
there is a request made and matches the URL pattern defined by us, it will
automatically direct to the specified controller and function.
A URL string and its corresponding controller class or method are in a one-to-
one relationship here. The URI segments usually follow this pattern:
example.com/class/function/id/ . All routing rules are defined in the
application/config/routes.php file of CodeIgniter.
/css/styles.css
/js/query.php
/img/news/566.gpg
$this->load->helper(
array('helper1', 'helper2', 'helper3')
);
Built-in libraries: It comes with various types of default helpers for multiple
things including strings, arrays, cookies, directories, file handling, and forms
among others.
Data abstraction: You can make use of the CodeIgniter database abstraction
layer for creating, adding, deleting, and replacing statements in a hassle-free
manner. This framework allows you to manage multiple connections using a
single application.
Active Developer Community: Bigger the community, the better the help you
get. Newly graduated developers look forward to the framework’s forum to get
their doubts solved and learn about new things in the process. With so many
people actively participating in it from around the world, your doubts will be
solved within few hours. And due to the same reason, CodeIgniter
documentation is 10 times bigger than any other framework.
Collaboration with Expression Engine: The collaboration permits developers
using CodeIgniter to use libraries and everything else provided by Expression
Engine and vice versa. Because of this, developers will get few benefits like
better parser class, improved built-in user authentication, and easy access to
modular applications.
Security: The security strength modification can be done according to your
client’s needs. These changes are made when the system is initialized by
switching off the magic_quotes_runtime directive irrespective of the
register_globals directive. You don’t need to remove the slashes during
information retrieval from the database. You can enable encryption of cookies,
where you can handle databases and escape SQL queries directly.
Immigration Features: Database schema update management is easier over
different fields by using the migration aspect. It is an easier process to immigrate
from the server to the server in Codeigniter.
Easy to Use: It is easier to use compared to other popular frameworks such as
Symfony, Zend framework, and Cake PHP.
$hook['pre_controller'] = array(
'class' => 'ExampleClass',
'function' => 'Examplefunction',
'filename' => 'ExampleClass.php',
'filepath' => 'hooks',
'params' => array('mango', 'apple', 'orange')
);
$this->load->library(array(‘library1’, ‘library2’));
Development It is a good
and option for front-
It is easier to use
Template end developers
and there is no
and it comes
template engine
along with the
provided.
Blade template
engine.
Here, $uri represents a URI string, $title represents an anchor title and
$attributes represents an HTML attributes. It returns an HTML hyperlink (anchor
tag) of string type.
The first parameter can have any segments you would like to append to the URL.
These segments can be a string or an array.
The second parameter is the text that will be displayed with a link. The URL will be
used in case you leave it blank.
The third parameter can contain an attribute list you would like added to the link.
The attributes can be a string or an associative array.
Example:
$email = \Config\Services::email();
$email->setFrom('[email protected]', 'Your Name');
$email->setTo('[email protected]');
$email->setCC('[email protected]');
$email->setBCC('[email protected]');
$email->setSubject('Email Test');
$email->setMessage('Testing the email class.');
$email->send();
CodeIgniter enables you to develop error reporting into your applications by using
the below-given functions. Also, it has a class dedicated to error logging that permits
messages related to error and debugging to be saved as text files.
Functions related to error handling are:
This function will display the error message provided by the
application/errors/errorgeneral.php template.
This function shows the 404 error message supplied to it by using the
application/errors/error404.php template.
show_404(‘page’ [, ‘logerror’])
This function permits you to write messages onto your log files. You must
provide anyone among three “levels” in the first parameter that indicates the
message type (debug, error, info), with the message itself in the second
parameter.
log_message(‘level’, ‘message’)
http://servername/controllerName/controllerFunction/parameter1/parameter2/.../parameter
Example:
interviewbit.com/user/edit/suresh
$this->load->model('name_of_the_model');
Include the relative path from the directory of your model, if your model is
placed inside a sub-directory. Consider an example, you have a model which is
placed at application/models/blog/AllPosts.php you can load it by using:
$this->load->model('blog/AllPosts');
You can access the methods provided by the model, once the model gets loaded
by using an object which has the same name as your controller:
Model:
Generally, a model is used for database interaction. When a user raises a
request for the specific data from the application, the model takes the
accountability to fetch out the records from the database table.
Also, a data structure represented by the model can be used to perform
several operations like retrieve, insert, update, and delete.
Controller:
The working of the CodeIgniter application is controlled by the controller. It
acts as an intermediary for the communication between the model and the
view. Therefore, it has the accountability to receive the user request and
handle that request by furnishing a result generated by the model. The
appropriate records will be displayed to the user by using the view
component. (Note: The Controller file name and class name should be the
same and must be in uppercase letters. Example- Main.php)
View:
Typically, a view is similar to a web page that has the information displayed
to the user. A view can also be an integral part of a web page such as header
and footer. The view page can be represented in both RSS(RDF Site
Summary) and a user interface.
<html>
<head>
<title>Blog</title>
</head>
<body>
<h1>Welcome to Blog in CodeIgniter</h1>
</body>
</html>
<?php
class Blog extends CI_Controller
{
public function index()
{
$this->load->view('ciblogview');
}
}
?>
Arrays are displayed as a brick[‘…’] and objects as an arrow(->). You are allowed to
access an array with the brick[‘…’] and object using the arrow (->). Therefore, add the
below-given code in the view file:
<?php
if (isset($mega_header)){
foreach ($mega_header as $key) {
?>
<div class="header_item">
<img alt="<?php echo($key['title']); ?>" src="<?php echo($key->img); ?>"
</div>
<?php
}
}
?>
$data = array(
'title' => 'TitleValue',
'heading' => 'HeadingValue'
);
$this->load->view('ciblogview', $data);
<?php
class Blog extends CI_Controller {
public function index()
{
$data['title'] = "TitleValue";
$data['heading'] = "HeadingValue";
$this->load->view('ciblogview', $data);
}
}
?>
<html>
<head>
<title><?php echo $title;?></title>
</head>
<body>
<h1><?php echo $heading;?></h1>
</body>
</html>
$sessiondata = array(
'name_of_user' => 'lekha',
'email' => '[email protected]',
'log_state' => TRUE
);
$this->session->set_userdata($sessiondata);
If you want to add a single user data at a time, set_userdata() supports this syntax:
$this->session->set_userdata('demo_username', 'demo_value');
$this->session->unset_userdata('name_of_user');
application: This directory will have your application logic. All of your
application codes will be held in this directory. Internal subdirectories in the
CodeIgniter directory structure are given below:
cache – It stores cached files.
config – It keeps configuration files.
controller – All application controllers are defined under this controller.
core – It consists of custom core classes that extend system files. For
example, if you create a base controller that other controllers should
extend, then you should place it under this directory.
helpers – This directory will be used for user-defined helper functions.
hooks – It is used for custom hooks in the CodeIgniter folder structure.
language – It is used to store language files for applications that use
multiple languages.
libraries – It is used to store custom-created libraries.
logs – Application log files are placed in this directory.
models - All application models must be defined under this directory.
third_party – This is used for custom many packages that are created by
you or other developers.
views – application views will be stored in this directory.
system: It consists of the framework core files. It is not advised to make any
modifications in this directory or put your own application code into this
directory. System subdirectories in CodeIgniter are given below:
core – This is considered to be the heart of the CodeIgniter Framework. All
of the core files that construct the framework are located here. If you would
like to extend the core file functionality, then you must
create a custom core file in the application directory. A er this, you are
allowed to override or add new behavior that you wish. You should never
make any changes directly in this directory.
database – It stores the files such as database drivers, cache, and other files
that are needed for database operations.
fonts – This directory contains fonts and font-related information.
helpers – This directory consists of helper functions that come out of the
box.
language – It contains language files that are used by the framework
libraries – It contains the source files for the different libraries that come
along with CodeIgniter out of the box.
user guide: This directory consists of a user manual for CodeIgniter. You should
Page 30 © Copyright by Interviewbit
CodeIgniter Interview Questions
$data = $this->security->xss_clean($data);
This function is used only when you are submitting data. The second Boolean
parameter is optional and used to check the image files for the XSS attacks. This is
very useful for file upload. If its value is true, that means the image is safer and not
otherwise.
From the SEO(Search Engine Optimization) point of the view, to make URL SEO
friendly and obtain more user visits.
Hide some URL elements like controller name, function name, etc. from the
users for security purposes.
Provides different functionality to the specific parts of a system.
Conclusion:
CodeIgniter has become the predominant choice of interest for developers while
creating websites. Even though there are plenty of other choices when considering
the right programming framework for the applications, CodeIgniter has gained its
own preference in the market. The sole reason that most of the developers are
choosing it is because of the several advantages that it offers to the people.
Freedcamp, Nissan, Bonfire, Buffer, etc. are some of the websites that use the
CodeIgniter framework. There is a great amount of demand for PHP developers with
CodeIgniter skills.
This set of interview questions will definitely help you excel in the CodeIgniter job
interview. We hope these interview questions and answers on CodeIgniter will assist
you to crack the relevant interview.
References:
“Professional CodeIgniter” by Thomas Myer.
“CodeIgniter for Rapid PHP Application Development” by David Upton.
Codeigniter Guide
Codeigniter Download
Css Interview Questions Laravel Interview Questions Asp Net Interview Questions