CodeIgniter Interview Questions – Set 02

How can you add or load a model in CodeIgniter?

To load models in controller functions, use the following function:

$this->load->model(‘ModelName’);
If in case your model file is located in sub-directory of the model folder, then you have to mention the full path. For example, if your file location is application/controller/models/project/ModelName. Then, your file will be loaded as shown below,

$this->load->model(‘project/ModelName’);

What are the hooks in CodeIgniter?

The Hook is a feature in CodeIgniter that provides a way to change the inner working of the framework without hacking the core files. It facilitates you to execute a script with a particular path within the CodeIgniter. Usually, it is defined in the application/config/hooks.php file.

How to enable CodeIgniter hook?

To enable hook, go to application/config/config.php/ file and set it TRUE as shown below,

$config[‘enable_hooks’] = TRUE;

What is an inhibitor of CodeIgniter?

In CodeIgniter, Inhibitor is an error handler class that uses native PHP functions like set_exception_handler, set_error_handler, register_shutdown_function to handle parse errors, exceptions, and fatal errors.

How can you print SQL statement in CodeIgniter model?

$this>db>insertid();

What is CodeIgniter?

CodeIgniter is an open source and powerful framework used for developing web applications on PHP. It is loosely based on MVC pattern and similar to Cake PHP. CodeIgniter contains libraries, simple interface and logical structure to access these libraries, plug-ins, helpers and some other resources which solve the complex functions of PHP more easily maintaining high performance. It simplifies the PHP code and brings out a fully interactive, dynamic website at a much shorter time.

Where is a newly created library stored in CodeIgniter structure?

It should be placed in application/libraries folder.

What is a token method in a CSRF attack?

To protect from CSRF, we need to connect both HTTP requests, form request and form submission. There are several ways to do this, but in CodeIgniter hidden field is used which is called the CSRF token. The CSRF token is a random value that changes with every HTTP request sent.

With each request, a new CSRF token is generated. When an object is created, name and value of the token are set.

$this->csrf_cookie_name = $this->csrf_token_name;
$this->_csrf_set_hash();
The function for it is,

function _csrf_set_hash()
{
if ($this->csrf_hash == ”)
{
if ( isset($_COOKIE[$this->csrf_cookie_name] ) AND
$_COOKIE[$this->csrf_cookie_name] != ” )
{
$this->csrf_hash = $_COOKIE[$this->csrf_cookie_name];
} else {
$this->csrf_hash = md5(uniqid(rand(), TRUE));
}
}
return $this->csrf_hash;
}

How can you connect models to a database manually?

To connect database manually use following syntax,

$this->load->database();

What are different types of hook points in CodeIgniter?

A list of different types of hook points in CodeIgniter:

  • post_controller_constructor – It is called immediately after your controller is started but before any method call.
  • pre_controller – It is called immediately before your controller being called. At this point, all the classes, security checks, and routing have been done.
  • post_sytem – It is called after the final page is sent to the browser at the end of the system execution.
  • pre_system – It is called much before the system execution. Only benchmark and hook class have been loaded at this point.
  • cache_override – It enables you to call your function in the output class.
  • display_override – It is used to send the final page at the end of file execution.
  • post_controller – It is called immediately after your controller is entirely executed