Understanding MVC Architecture in CodeIgniter : Day 2

MVC Architecture in CodeIgniter

Welcome back to Day 2 of our journey to build a portfolio website with CodeIgniter! Today, we’ll explore the foundational concept of MVC Architecture in Codeigniter (Model-View-Controller) architecture and how it applies to CodeIgniter.

So Start MVC Architecture in CodeIgniter..

What is MVC?

MVC is a design pattern widely used in web development to separate application logic from presentation. Here’s a brief overview of each component:

  • Model: Represents the data structure. It interacts with the database, performs data manipulation, and contains business logic.
  • View: Represents the presentation layer. It displays data to users and gathers user input (e.g., HTML templates).
  • Controller: Acts as an intermediary between the Model and View. It processes user requests, retrieves data from the Model, and passes it to the View.
Compalgo Labs Offering School Management software

MVC in CodeIgniter

CodeIgniter follows a straightforward implementation of the MVC pattern:

  • Model: Responsible for database operations such as fetching, inserting, updating, and deleting data. Models are stored in the application/models directory.
  • View: Represents the HTML files that display the data. Views are stored in the application/views directory.
  • Controller: Handles user requests, retrieves data from models, and loads views. Controllers are stored in the application/controllers directory.

Example: Creating a Hello World Page

Let’s create a simple “Hello World” page to illustrate MVC in action:

Step 1: Create a Controller

Navigate to application/controllers/ and create a new file named Hello.php. Add the following code:

<?php
class Hello extends CI_Controller {
    public function index() {
        $data['message'] = "Hello, world!";
        $this->load->view('hello_view', $data);
    }
}
?>

Step 2: Create a View

Navigate to application/views/ and create a new file named hello_view.php. Add the following code

<!DOCTYPE html>
<html>
<head>
    <title>Hello World</title>
</head>
<body>
    <h1><?php echo $message; ?></h1>
</body>
</html>

Step 3: Access the Page

Open your web browser and go to http://localhost/your_project_name/index.php/hello. You should see “Hello, world!” displayed on the page.

Benefits of MVC

  • Separation of Concerns: MVC separates application logic, user interface, and data manipulation, making code more organized and maintainable.
  • Code Reusability: Models and Views can be reused across different parts of the application, promoting efficient development.
  • Scalability: MVC facilitates scalability by allowing easier modifications and additions to different components of the application.

Conclusion

Understanding MVC architecture lays a solid foundation for building robust web applications with CodeIgniter. In Day 3, we’ll dive deeper into setting up the database for our portfolio website and defining the data structure.

Keep practicing and experimenting with CodeIgniter! Feel free to leave comments or questions below. Happy

Not downloded yet can downlod Codeigniter here!

About The Author

Leave a Comment

Your email address will not be published. Required fields are marked *