Codeignitor is based on MVC design pattern. We will see how the controller, model, and view work and display the result to the user.
Controller: Like the name suggest, it controls the communication between views and models. All the URL works in CodeIgniter with controller name. Now let’s see some point which we need to keep in mind when we create a new controller.
- When we create a class in a controller the name of the class should match with the file name and it should start with upper case else we will receive an error while running our project.
- Once the class is declared we inherit all the properties of CI_Controller with extends keywords to use all the properties of CI_Controller(Go through the PHP | Common terminology in OOP to understand inheritance). There won’t be any issue if we won’t extend CI_Controller but in that case, we won’t be able to access any property of CI_Controller.
- Every controller has a default function index which runs automatically when we define only controller name in URL. For the rest of the functions, we need to mention it manually after controller name in URL.
Model: Model is responsible to handle the queries related with backend. All the queries or functionalities of the backend like database, APIs we write in models. Now let’s see some point which we need to keep in mind when we create a new model.
- When we create a class in a model the name of the class should match with the file name and it should start with upper case else we will receive an error while running our project.
- Once the class is declared we inherit all the properties of CI_Model with extends keywords to use all the properties of CI_Model
- When we create a function in model make sure it should be public else it won’t be accessible in controller.
- To access the model in a controller it need to be loaded first, after loading the model we can call the function of that specific model in controller. Below is the line to load the model in a controller.
Below is the line to load model in autoload.php which is available in config folder . If we need the model globally in our project, then use autoload.php.
$autoload['model'] = array('post_model');
Keeping all the above point in mind we will see an example of how MVC works in CI.
Controller File: Create a file users.php in Controller folder and write down the code below.
Model File: Create a file usermodel.php in Model folder and write down the code below.
View File: Create a file users_list.php in View folder and write down the code below.
Now run http://localhost/ci_tutorial/index.php/Users in the browser.
- Helpers in Codeignitor
- Libraries in Codeignitor
- Introduction to Codeignitor (PHP)
- What is the use of number after “break” or “continue” statements in PHP ?
- How to display popup message when logged out user try to vote ?
- How to create a dynamic JSON file by fetching data from localserver database ?
- How to unzip a file using PHP ?
- Scope Resolution operator in PHP
- PHP | get_object_vars() Function
- PHP | get_declared_classes() Function
- CakePHP Framework | Installation and Configuration
- How to fetch data from localserver database and display on HTML table using PHP ?
- Difference Between Laravel and CodeIgniter Framework in PHP
- How to download a CSV file in PHP that is triggered through a URL ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.