Framework

MVC Framework Introduction

Oct 6, 2024, 9:23 AM
Blog Thumbnail

When Building Web Applications

Most developers make use of design strategies that enable easy maintenance, growth, and also consider specific areas of the application separately. One of the best-known design structures working for many years already is MVC, which stands for Model-View-Controller. Two major advantages of the MVC structure are the ease of managing and developing complex applications by organizing the logic of the application into three distinct components.

What is MVC?

MVC (Model – View – Controller) is a design pattern for designing user applications, created at Ohio State University by Dr. Trygve Reenskaug in the late 1970s. It stratifies the application into three main interacting elements: model, view, and controller.

The Components of MVC

1. Model

The Model is the specific part of the application that includes the data and also handles the business side of the application. This component deals directly with either the database or other data sources to obtain, save, and process pieces of data.

Responsibilities:

  • Take care of the application’s data logic.
  • Carry out CRUD functions.
  • Follow the business principles and validation.
// Laravel Model Example
namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Post extends Model {
    protected $fillable = array('title','content');
}

                    

2. View

The View is responsible for presenting the data to the user. It acts as the interface, rendering the content and handling user interactions visually.

Responsibilities:

  • Display data provided by the Model or Controller.
  • Ensure a clean and user-friendly presentation layer.
<!-- Laravel View Example -->
<ul>
    @foreach ($posts as $post)
        <li>{{ $post->title }}</li>
    @endforeach
</ul>

                    

3. Controller

The Controller represents the understanding of communication between the Model and the View. It accepts inputs from the user, communicates with the Model, and chooses a View to be presented.

Responsibilities:

  • Request and respond to the users’ actions.
  • Retrieve content from the Model and push it to the View.
  • Direct application flow and logic.
// Laravel Controller Example
namespace App\Http\Controllers;

use App\Models\Post;

class PostController extends Controller {
    public function index() {
        // Fetching all posts
        $posts = Post::all();
        // Sending posts to posts.index view
        return view('posts.index', compact('posts'));
    }
}

                    

How MVC Works Together

  1. The user makes an action with the application, for example, pressing a button.
  2. The Controller handles the action and makes the Model involved.
  3. The Model creates or seeks the objects.
  4. The View prepares the information and presents it to the user.

Popular MVC Frameworks

some framework used MVC ..

  • Ruby on Rails
  • Django
  • CherryPy
  • Spring MVC
  • Catalyst
  • Rails
  • Symphony
  • Laravel
  • Fuel PHP
  • Zend Framework

Advantages of MVC

  • Separation of Concerns: Each component has its own definition.
  • Reusability: Codes can be used again in the entire application.
  • Scalability: Make more features without changing the existing ones.
  • Testability: Non-interacting logic makes it a good candidate for testing.
  • Collaboration: Different components can be worked on at the same time by teams.

Disadvantages of MVC

  • mvc structure not suiytable for small project like small websites.
  • The inefficiency of data access in view.
  • Increased complexity and Inefficiency of data
  • It is difficult to read model , change, test model , and reuse this model