Read Latest PHP Codeigniter Interview Questions From Below: Explain What Is Codeigniter?
Read Latest PHP Codeigniter Interview Questions From Below: Explain What Is Codeigniter?
Codeigniter interviews. Read about helpers, sessions, hooks, Routing, Constants ORM supported by
Codeigniter and more
CodeIgniter is a powerful MVC based PHP framework with a very small footprint, built for developers who
need a simple and elegant toolkit to create full-featured web applications.
As on Sept 3, 2018 CodeIgniter 3.1.9 is the latest version of the framework. You can download it from here
MySQL (5.1+) via the MySQL (deprecated), MYSQLI and PDO drivers
Oracle via the oci8 and PDO drivers
PostgreSQL via the Postgre and PDO drivers
MS SQL via the MsSQL, Sqlsrv (version 2005 and above only) and PDO drivers
SQLite via the SQLite (version 2), sqlite3 (version 3) and PDO drivers
CUBRID via the Cubridand PDO drivers
Interbase/Firebird via the iBase and PDO drivers
ODBC via the ODBC and PDO drivers (you should know that ODBC is actually an abstraction layer)
As the name suggests, helpers help you with tasks.Each helper file is simply a collection of functions in a
particular category.They are simple, procedural functions.Each helper function performs one specific task, with
no dependence on other functions.
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.
$this->load->helper('name');
Where name is the file name of the helper, without the .php file extension or the “helper” part.
Read More
In Software engineering routing is the process of taking a URI endpoint (that part of the URI which comes after
the base URL) and decomposing it into parameters to determine which module, controller, and action of that
controller should receive the request.In Codeigniter typically there is a one-to-one relationship between a URL
string and its corresponding controller class/method.The segments in a URI normally follow this pattern:
example.com/class/function/id/.In CodeIgniter, all routing rules are defined in your
application/config/routes.php file.
CodeIgniter’s Hooks feature provides a way to modify the inner workings or functionality of the framework
without hacking the core files.
Read More
CodeIgniter uses a few functions for its operation that are globally defined, and are available to you at any point.
These do not require loading any libraries or helpers.
is_php($version)
is_really_writable($file)
config_item($key)
set_status_header($code[, $text = ”])
remove_invisible_characters($str[, $url_encoded = TRUE])
html_escape($var)
get_mimes()
is_https()
is_cli()
To set default timezone in Codeigniter open application/config.php file and add below code in it.
date_default_timezone_set('your timezone');
In Codeigniter, you can link images/CSS/JavaScript by using the absolute path to your resources.
RewriteEngine on
RewriteCond $1 !^(index\.php|[Javascript / CSS / Image root Folder name(s)]|r
RewriteRule ^(.*)$ /index.php/$1 [L]
In Codeigniter, models will typically be loaded and called from within your controller methods. To load a model
you will use the following method:
$this->load->model('model_name');
If your model is located in a sub-directory, include the relative path from your model’s directory. For example,
if you have a model located at application/models/blog/Posts.php you’ll load it using:
$this->load->model('blog/Posts');
Once your Model loaded, you will access its methods using an object with the same name as your controller:
Example
Q15. What are Sessions In CodeIgniter? How to read, write or remove session in
CodeIgniter?
Sessions in CodeIgniter
In CodeIgniter Session class allows you maintain a user’s “state” and track their activity while they are
browsing your website.
In order to use session, you need to load Session class in your controller.
$this->load->library('session');
$this->session
Usage
$this->session->userdata('your_key');
You can also read a session data by using the magic getter of CodeIgniter Session Class
Usage
$this->session->item
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.
Example
$newdata = array(
'username' => 'johndoe',
'email' => '[email protected]',
'logged_in' => TRUE
);
$this->session->set_userdata($newdata);
If you want to add userdata one value at a time, set_userdata() also supports this syntax:
$this->session->set_userdata('some_name', 'some_value');
Removing Session Data
Session’s Class unset_userdata() method is used to remove a session data in CodeIgniter. Below are example
usages of same.
$this->session->unset_userdata('some_key');
Usage:
function add_post($post_data){
$this->db->insert('posts', $post_data);
$insert_id = $this->db->insert_id();
return $insert_id;
}
Usage:
//Loading Cart library
$this->load->library('cart');
$data = array(
'id' => 'sku_9788C',
'qty' => 1,
'price' => 35.95,
'name' => 'T-Shirt',
'options' => array('Size' => 'L', 'Color' => 'Red')
);
$this->cart->insert($data);
Q18. What is the CLI? Why we use CLI in Codeigniter?
CLI is a text-based command-line interface for interacting with computers via a set of commands.
Syntax :
Parameter:
Sample Usage:-
if ($this->db->field_exists('field_name', 'table_name'))
{
// some code...
}
In Codeigniter is_cli() method is used to check request is from the command line or not.
Returns TRUE if the application is run through the command line and FALSE if not.
Below images give you an understanding of how data flows throughout the system in Codeigniter.
Source: https://www.codeigniter.com/user_guide/overview/appflow.html
The default URL pattern in CodeIgniter consists of 4 main components. They are :
Object-relational mapping (ORM) is programming technique for converting data between incompatible type
systems using object-oriented programming languages. Below is the list of ORM’s supported by CodeIgniter
Framework
DataMapper
Doctrine
Gas ORM
Q30. Explain URL Helper? Can you list some commonly used URL helpers in
Codeigniter?
https://codeigniter-id.github.io/user-guide/helpers/url_helper.html
To autoload resources, open the application/config/autoload.php file and add the item you want loading to the
autoload array. You’ll find instructions in that file corresponding to each type of item.
Q33. In which directory logs are saved in Codeigniter? How to enable error logging in
Codeigniter?
By default, all logs in Codeigniter are stored in logs/ directory. To enable error logging you must set the
“threshold” for logging in application/config/config.php. Also, your logs/ must be writable.
$config['log_threshold'] = 1;
Error Messages. These are actual errors, such as PHP errors or user errors.
Debug Messages. These are messages that assist in debugging. For example, if a class has been
initialized, you could log this as debugging info.
Informational Messages. These are the lowest priority messages, simply giving information regarding
some process.
Csrf is used to set the protection in CodeIgniter. To set csrf, you have to put the corresponding config value
True.