Lesson 4 of 50 – Laravel Folder Structure
8%

Laravel Folder Structure

Laravel provides a predefined folder structure that helps developers organize routes, controllers, models, views, configuration, database files, and other application resources.

In this lesson, you will explore the important directories and files created when you install a Laravel application.

1. Overview of Laravel Project Structure

After creating a Laravel project named student-app, you will find a structure similar to the following. Some files and directories may differ between Laravel versions.

student-app/
|
|-- app/
|-- bootstrap/
|-- config/
|-- database/
|-- public/
|-- resources/
|-- routes/
|-- storage/
|-- tests/
|-- vendor/
|
|-- .env
|-- .env.example
|-- artisan
|-- composer.json
|-- composer.lock
|-- package.json
|-- phpunit.xml

Each directory has a particular responsibility. Understanding these locations helps you find the correct file when building an application.

Example:

  • Routes are usually defined in the routes/ directory.
  • Controllers are usually stored under app/Http/Controllers/.
  • Blade templates are usually stored under resources/views/.
  • Database migrations are usually stored under database/migrations/.
2. The app Directory

The app/ directory contains much of your application's PHP code, including controllers, models, middleware, and other application classes.

app/
|
|-- Http/
|   |-- Controllers/
|   |-- Middleware/
|
|-- Models/

Important locations:

  • app/Http/Controllers/ — HTTP controllers that handle requests and coordinate application logic.
  • app/Models/ — Eloquent models that represent and work with application data.
  • app/Http/Middleware/ — application middleware classes when created or provided by the installed Laravel structure.

For example, a student management application might have a StudentController.php controller and a Student.php model.

Example file paths:

app/Http/Controllers/StudentController.php

app/Models/Student.php
3. The routes Directory

The routes/ directory contains route definitions that connect incoming requests to application behavior.

Common route files:

  • web.php — web routes, commonly using session and cookie features.
  • console.php — console-related routes and definitions.
  • api.php — API routes when the API routing file is installed or configured.

The available route files depend on your Laravel version and project setup. For example, some newer Laravel applications do not include routes/api.php until API routing is installed.

Example: routes/web.php

<?php

use Illuminate\Support\Facades\Route;

Route::get('/students', function () {
    return 'Student List';
});

When a user opens the /students URL using a GET request, Laravel returns the text Student List.

4. The resources Directory

The resources/ directory contains view templates and frontend resources.

resources/
|
|-- views/
|   |-- welcome.blade.php
|
|-- css/
|-- js/

Important locations:

  • resources/views/ — Blade templates.
  • resources/css/ — frontend CSS source files in setups that use them.
  • resources/js/ — JavaScript source files in setups that use them.

Example view:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Student Page</title>
</head>
<body>
    <h1>Welcome to Student Management</h1>
</body>
</html>

Save the example as resources/views/students.blade.php. You can return it from a route with return view('students');.

5. The database Directory

The database/ directory contains files used to create, populate, and test your database.

database/
|
|-- migrations/
|-- seeders/
|-- factories/

What each directory does:

  • Migrations: Define changes to database tables and other schema objects.
  • Seeders: Insert initial or sample data into the database.
  • Factories: Generate model data for development and testing.

Example migration command:

php artisan make:migration create_students_table

Laravel creates a migration file in database/migrations/.

You will learn migrations, seeders, and factories in later lessons.

6. The public Directory

The public/ directory is the web entry point for a standard Laravel application. The web server should serve requests from this directory rather than exposing the entire project directory.

public/
|
|-- index.php
|-- robots.txt
|-- favicon.ico

The exact files vary by project. The key file is index.php, which serves as the entry point for HTTP requests.

Important security rule:

Do not configure your production web server to expose the project root. Doing so could reveal sensitive files such as .env, source code, and configuration files.

Uploaded user files should also be handled carefully. Laravel's storage system provides mechanisms for managing public and private files.

7. The config and bootstrap Directories

The config Directory

The config/ directory contains configuration files for different application services.

config/
|
|-- app.php
|-- database.php
|-- cache.php
|-- mail.php
|-- session.php

The exact files depend on the Laravel version. For example, config/database.php contains database connection settings.

The bootstrap Directory

The bootstrap/ directory contains files that initialize the Laravel application.

bootstrap/
|
|-- app.php
|-- cache/

The precise structure varies across Laravel versions. Avoid modifying bootstrap files unless you understand the configuration or customization you need.

8. Important Root Files

The project root contains several files that control dependencies, environment settings, and command-line operations.

File Purpose
.env Environment-specific settings and secrets.
.env.example Example environment configuration.
artisan Laravel command-line entry point.
composer.json PHP dependencies, package constraints, and Composer configuration.
composer.lock Exact resolved package versions for reproducible dependency installation.
package.json Frontend package scripts and JavaScript dependencies when used.

Keep real credentials and application secrets out of public repositories. In particular, do not publish the real .env file.

9. Practical Assignment

Complete these tasks to practise navigating a Laravel application.

  1. Open your Laravel project in a code editor.
  2. Find the app/ directory.
  3. Locate routes/web.php.
  4. Find the resources/views/ directory.
  5. Open a Blade view and identify its HTML.
  6. Locate the database/migrations/ directory.
  7. Find the public/index.php entry point.
  8. Locate composer.json and artisan.
  9. Explain the purpose of five Laravel directories in your own words.

Challenge:

Create a new view named student-details.blade.php in resources/views/ and display a heading that says "Student Details".

In the next lesson, you will learn how to use Laravel Artisan commands to create controllers, models, migrations, and other application components.


🧠 Quick Quiz

1. Where are Laravel Blade views normally stored?




2. Which directory normally contains Laravel migrations?




3. Which file is Laravel's HTTP entry point in a standard project?