What is MVC?
MVC is a three-tier architecture of application development approach.Almost the popular languages use this pattern. MVC provides Modular structure to an application. Three interconnected parts are Model, View, and Controller.
M: Model, that means data handling and information interchange. Implement CRUD.
V: view i.e. front end and representational parts, where user interacts with machine
C: Controller, The business login, handle user request an passes to model and in return provide results to the user.
What is difference b/w MVC and HMVC?
HMVC is a hierarchical MVC, The whole application is modularized in interconnected modules and each module has its own MODEL, VIEW, CONTROLLER. In HMVC a module is a complete package of User Interface, business logic, and data handling. So MVC is discrete whereas HMVC contains MVC at each level.
How we can load a model in CI controller?
We can call load a model in the constructor as given in the following example:
1
2
3
4
|
//call model $this ->users->add_new_user( $data ); ?> |
We can also assign an alias to ci_model. We can use ‘users’ to call a model rather than calling ‘users_model’.
What is autoload.php in the config?
This is a file where preloaders are defined. Suppose we want to include database library, any model, URL library, sessions etc, then we can assign all these things here.
What helpers do in CI?
Helpers are the simple PHP functions returning some values and we can call them anywhere in our project once we initialized them.
What are the libraries in CI, explain some popular libraries?
The library is the packages which are designed to implement a particular set of functionalities. For example Database. Once we Initialized it, we do any operation on the database.
What are Hooks?
Hooks provide flexibility to CI core. We can modify the core functionality of CI without disturbing core files and use hooks.Learn more about Hooks in CodeIgniter
How we define URL and redirect in CI?
We use URL helper and then we can create redirect links :
1
|
|
these functions provide the way to create URL.
How to write a SELECT query in CI model?
We can write a select query in code igniter using the standard conventions of CI as given below:
1
2
3
4
|
$query = $this ->db->get(); $result = $query ->result(); ?> |
How to call a CI model Controller?
The following example illustrates, how to call a model in code igniter controller:
1
2
3
4
|
//call model $this ->users->add_new_user( $data ); ?> |
What are the views?
A view is the display part of the application. It renders the results on screen and provides UI.We can assemble views files in directories as well.
How we define controller in CI?
A controller is the backbone of MVC. It creates a connection between model and view.We can define a controller as illustrated:
1
|
|
How controllers and models are different?
Controllers deals with business logic. When user Instruct the machine to do something, controller listens to that response while interaction another side with a model for data. Whereas a model is data handling layer. It saves data to the database and it requests data from the database that further goes to the controller to display on view.
What is the convention to call controller function with a parameter in CI?
We can call a controller function in uri using the a pattern given below. First part of URI is base URL. Base URL followed by controller route and followed by function name , parameters.
base_url/controller_name/controller_function/param1/param2
[Controller Class]/[controller methos]/[parameters]
How to write JOIN Query in Code Igniter?
Code Igniter has a very rich Database library. It provides a flexible way to call and create logics. Either we can use raw queries or we can use join() function.
$this->db->select('t1.name,t2.email'); Â Â Â Â $this->db->from('table1 as t1'); Â Â Â Â $this->db->join('table2 as t2', 't1.id = t2.user_id', 'left'); Â Â Â Â $query = $this->db->get(); Â Â Â Â return $query->result(); ?>
How to apply where clause in Code Igniter?
Using Code Igniter Database library function ‘where()’ we can apply where clause.
1
2
3
4
5
6
7
8
|
'status' => 'active' ); $this ->db->select( 'name,email,mobile' ); $this ->db->get( 'users' ); $query = $this ->db->where( $where ); $result = $query ->result(); ?> |
What is routing?
Routing is a way to define URI and how to call controller methods with a particular URI.
Routing provides flexibility to change the way we call controller functions. We can rename controller and functions. This enhances the security features as well as we can create SEO friendly URLs.
What is the way to fetch query results in CI?
Ther are many ways to fetch result in Code Igniter. Some of them are as follows:
result();
result_array();
row();
row_array();
unbuffered_row();
How we call controller method using AJAX?
Simply we can call the URL using proper method and parameters, and then handle those request on a controller and if required we can render a view.
How to define a session variable in CI?
First of all, we need to load session library and then we can call some library functions to accomplish this task:
1
2
3
4
|
//define a session variable named 'user_id' $this ->session->set_userdata( 'user_id' , $user_id ); ?> |