A Visual Guide to Directory Structures
Where do you put your files? How does Laravel know? These are a few questions that I normally get from individuals just starting out with Laravel, or really any other framework. Laravel creates a folder structure for you and it expects certain files to appear in specific places. If they’re not there, Laravel will not know what to do. It expects your controllers to be in the Http
folder, it expects your models to be in the Models folder, etc. If you’re not familiar with those concepts yet, don’t worry, we’ll cover each of them in length throughout this series. For now, lets look at some of the folders that we’ll spend most of our time in.
app/
Console/
Exceptions/
Http/
Controllers/
UserController.php
Middleware/
Models/
User.php
Providers/
bootstrap/
config/
database/
factories/
UserFactory.php
migrations/
2014_10_12_000000_create_users_table.php
...
seeders/
DatabaseSeeder.php
lang/
public/
resources/
...
views/
welcome.blade.php
routes/
api.php
...
web.php
storage/
...
logs/
tests/
Feature/
ExampleTest.php
Unit/
ExampleTest.php
vendor/
.env
.env.example
...
The items in bold above are the folders that we’ll cover in this article. We’ll make our way through the remainder of the directory as we increase our understanding of Laravel. The absolute bare minimum folder structure that you should know to get started with Laravel is:
app/Http/Controllers
app/Models
database/migrations
resources/views
routes
However, since I’ve highlighted a few more, let’s cover those as well.
app/Http/Controllers
is the place where all of your controllers go. This is an MVC framework (Model View Controller) and our controllers will end up here. There’s a great artisan command for creating these files.
app/Models
contains the, you guessed it, models. These are the files that will communicate with the database and have a ton of built in functionality out of the box.
resources/views
the last component of the MVC framework is the view. These are your html files: what the user sees. Laravel allows for blade components to be injected into these files, which we’ll look at, that completely removes the necessity to include actual php code into the mix.
database/migrations
whenever we want to create a new table in the database, we don’t log in and create tables directly, we instead use the migrations.
database/seeders
Laravel offers the ability to seed your database. So, if you wanted to put thousands of rows of data quickly into your newly created tables, Laravel has that option. The seeders folder will contain all of your seeders.
database/factories
Factories work closely with seeders. Instead of manually generating the thousand entries for your table, you can create a factory that knows how to, for example, generate a phone number and a random first and last name. This is one of my favorite things in Laravel and we’ll definitely go over it in detail.
routes
This folder contains two really important files: api.php
and web.php
. When a user types into a url, the route file is the file that gets contacted first. We know that the user clicked on the about page link, https://example.com/about. What do we do now? In this instance, the web.php
file will take care of routing the user to the appropriate controller that will return the view. The api.php
works with API requests.
storage/logs
we’ll touch on this folder primarily because of the Logs file that’s generated when errors occur in the application.
tests
This is the folder that will contain your unit and feature tests. We’ll look at creating unit and feature tests later. Just like most of the files that we’ll create, we’ll use the artisan
command to generate these tests.
app
asside from holding all of the other folders that we just discussed, it also contains the .env
file, which is crucial for Laravel.
I know that you can start digging through the various different directories and it can all seem very overwhelming. Realistically, the folders that we just outlined are more than enough to get us to quite a good portion of this series. We’ll explore more advanced topics throughout this series and will highlight the correct folder location as we touch on those topics. Let’s make this simple as we tackle Laravel.
Laravel Series
Continue your Laravel Learning.
Get Ready to Docker
Efficient Laravel Installation with Docker: Streamline Your Development Environment for Professional Web Projects.
A Visual Guide to Directory Structures
Laravel – P3: Directory Structure Basics
Where do you put your files? How does Laravel know? These are a few questions that I normally get from individuals just starting out with Laravel.
How To Route in Laravel
Laravel – P4: Basic Routing and View Introduction
How do you get to a specific view in Laravel without going through a Controller? It can be done through the route itself and we’ll explore that in this article.