DEV Community

Adebayo Olukunle
Adebayo Olukunle

Posted on

Laravel File Structure and MVC Architecture

Laravel is one of the most popular PHP frameworks, known for its elegant syntax and robust features. It follows the Model-View-Controller (MVC) architecture, which helps developers organize code efficiently. Understanding Laravel’s file structure is crucial for developing scalable applications. In this article, we’ll explore Laravel’s directory structure and its implementation of the MVC architecture.

Laravel File Structure

When you install Laravel, the framework creates a structured directory layout. Below is an overview of the key directories and files in a Laravel project:

1. Root Directory

  • app/: Contains the core application logic, including Models, Controllers, and Middleware.

  • bootstrap/: Stores files required for the initial framework bootstrapping, including the cache directory for optimized application performance.

  • config/: Contains configuration files for services like database connections, authentication, mail, and more.

  • database/: Includes database migrations, seeders, and factories.

  • public/: The entry point of the application (index.php). Also stores assets like CSS, JavaScript, and images.

  • resources/: Contains view templates, raw assets, and language files.

  • routes/: Defines application routes (web, API, console, and channels).

  • storage/: Stores logs, session files, compiled views, and cache files.

  • tests/: Holds unit and feature tests.

  • vendor/: Contains third-party dependencies managed by Composer.

1. Key Files

- artisan: Laravel’s command-line interface for executing tasks.

- composer.json: Defines dependencies for the application.

- .env: Stores environment-specific configuration variables.

- server.php: Allows the application to be run using PHP’s built-in server.

Laravel's MVC Architecture

Laravel follows the MVC architecture pattern, which separates the application into three main components which are Model, View, Controller. This means that when the request gets into the application route, the route passes it to the Controller, the controller accesses the Model which is the data layer, and then the Controller also passes the data from the Model to the View for the users.

1. Model (app/Models/)

The Model represents the data structure and business logic. It interacts with the database using Eloquent ORM (Object-Relational Mapping).

Example:

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
    protected $fillable = ['name', 'price', 'description'];
}
Enter fullscreen mode Exit fullscreen mode

Object-relational mapping (ORM) is a technique that enables data to be easily mapped between a relational database management system (RDBMS) like MySQL database, and an object-oriented programming (OOP) language. ORM systems serve as a bridge between the two different paradigms.

2. View (resources/views/)

Views handle the presentation layer of the application. Laravel uses Blade templating engine to create dynamic content.

Example:

<!DOCTYPE html>
<html>
<head>
    <title>Product List</title>
</head>
<body>
    <h1>Products</h1>
    <ul>
        @foreach ($products as $product)
            <li>{{ $product->name }} - ${{ $product->price }}</li>
        @endforeach
    </ul>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

3. Controller (app/Http/Controllers/)

Controllers handle the application logic and bridge the Model and View.

Example:

namespace App\Http\Controllers;

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

class ProductController extends Controller
{
    public function index()
    {
        $products = Product::all();
        return view('products.index', compact('products'));
    }
}
Enter fullscreen mode Exit fullscreen mode

4. Routes (routes/web.php)

Routes define how the application responds to user requests. It follows the http verb methods to receive the request, and maps the controller class and methods which would handle the request.

Example:

use App\Http\Controllers\ProductController;

Route::get('/products', [ProductController::class, 'index']);

Route::post('/products', [ProductController::class, 'create']);
Enter fullscreen mode Exit fullscreen mode

Conclusion

Understanding Laravel’s file structure and MVC architecture is essential for building efficient applications. The framework's well-organized directory structure and adherence to MVC principles make development streamlined and maintainable. By leveraging Models, Views, and Controllers effectively, developers can create scalable web applications with Laravel.

Top comments (0)