Understanding Laravel MVC Architecture

Tutorial 2 of 5

Understanding Laravel MVC Architecture

1. Introduction

In this tutorial, we are going to delve into the MVC (Model-View-Controller) architecture of Laravel. The MVC architecture is a crucial part of the Laravel framework that helps in maintaining the code's scalability and manageability.

By the end of this tutorial, you will understand how data is processed and displayed in a Laravel application. You will also learn the roles and responsibilities of Models, Views, and Controllers in Laravel.

Prerequisites:
- Basic knowledge of PHP
- Familiarity with OOP (Object-Oriented Programming)
- Basic understanding of Laravel

2. Step-by-Step Guide

The MVC architecture is a design pattern that separates an application into three interconnected components: Model, View, and Controller.

Model: The Model represents your data structures. It is responsible for retrieving and storing data in your database.

View: The View is responsible for the graphical user interface. It displays the data provided by the Model in a user-friendly format.

Controller: The Controller is the connection between the Model and the View. It processes the HTTP requests, manipulates data using the Model, and returns the results to the View.

Best Practices:

  • Keep your controllers slim. Controllers should only contain methods related to HTTP requests.
  • Fat models. All business logic should be in the model.
  • Views should only contain display logic.

3. Code Examples

Let's create a simple blog application to understand the MVC architecture in Laravel.

Model:

// app/Models/Post.php
namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    protected $fillable = ['title', 'body'];
}

Controller:

// app/Http/Controllers/PostController.php
namespace App\Http\Controllers;

use App\Models\Post;
use Illuminate\Http\Request;

class PostController extends Controller
{
    public function index()
    {
        $posts = Post::all();
        return view('posts.index', ['posts' => $posts]);
    }
}

In the controller, we have defined an index method, which retrieves all the posts from the database and passes them to the view.

View:

<!-- resources/views/posts/index.blade.php -->
<!DOCTYPE html>
<html>
<head>
    <title>Blog Posts</title>
</head>
<body>
    @foreach ($posts as $post)
        <h2>{{ $post->title }}</h2>
        <p>{{ $post->body }}</p>
    @endforeach
</body>
</html>

In the view, we are displaying the posts in a user-friendly format.

4. Summary

In this tutorial, we have learned about the MVC architecture of Laravel. We discussed the roles of Models, Views, and Controllers in a Laravel application and created a simple blog application to illustrate these concepts.

After grasping the basics of MVC architecture, you can proceed to learn about Laravel routing, middleware, and other advanced features.

5. Practice Exercises

  1. Create a new controller called UserController and a User model. Write a method in the controller that retrieves all users from the database and passes them to a view.

  2. Modify the UserController to include a method that receives a user id as a parameter, retrieves the corresponding user from the database, and passes it to a view.

  3. Create a new method in the UserController that receives a request to create a new user. Validate the request, create the user in the database, and redirect the user back to the users' list.

Remember, the best way to learn is by doing. Happy coding!