Skip to content

Directory Structure

Getting Started

Directory Structure

Introduction

A new Laravel application already has a place for your routes, handlers, models, and page components, so you, your teammates, and the agents that write code with you know where to find each part.

As you work, a few paths appear that Git ignores: the app binary that go build writes, node_modules/, the assets Vite builds into public/build/, the SSR bundle in bootstrap/ssr/, and your .env file.

The root directory

Path What it holds
go.mod, go.sum Your module's name and its dependencies, including Laravel.
main.go Your program's entry point, which builds your application and runs it.
.env Your environment configuration, such as your database URL and encryption key.
package.json Your frontend dependencies and the npm scripts, such as npm run dev.
vite.config.ts Vite's configuration: its plugins, your frontend's entry point, and the formatting and lint rules that npm run check applies.
database/migrations/ Your migrations, one Go file per change.
database/schema/ Your schema files, which schema:dump writes when you squash your migrations.
database/seeders/ Your seeders, which fill a database with rows.
database/factories/ Your model factories, which make models with fake values for your tests and seeders.
bootstrap/, routes/, handlers/, models/, and more Your application's Go packages.
resources/ Your frontend's source, such as page components and styles, and your views.
public/ Files your application serves as they are, such as favicon.ico and the assets in public/build/.
storage/ Files your application writes as it runs, such as uploads and logs.
bootstrap/ssr/ The server-side rendering bundle that npm run build:ssr builds.
tests/ Your tests.

go.mod names your module app, so your packages import one another by paths that start with app, as in app/models. A directory you add, such as billing, becomes the package app/billing.

The resources directory

The resources directory holds your frontend's source, which Vite builds into public/build/, and your views:

Path What it holds
resources/js/app.tsx Your frontend's entry point, which starts Inertia.
resources/js/pages/ Your Inertia page components. A handler renders one by name, so inertia.Render("Welcome", ...) renders Welcome.tsx. Error.tsx renders your application's error pages.
resources/views/app.html The root view, the HTML document that every page renders in. Your other views live in resources/views too.
resources/css/app.css Your styles, which import Tailwind.
resources/images/ Images your pages import. Vite bundles them with your other assets.
resources/resources.go The Views variable, which embeds resources/views in your binary, so you deploy a single file.

The storage directory

The storage directory holds the files your application writes, and Git ignores what's inside. storage/app/ is for files your application keeps, such as uploads. The public disk stores its files in storage/app/public, and Laravel serves them at /storage, so a user can download an avatar stored at avatars/1.png from /storage/avatars/1.png. The local disk keeps private files in storage/app/private.

In production, you'll usually store uploads in object storage instead, so every server sees the same files and they survive a redeploy. Laravel Cloud Object Storage works out of the box with nothing to install, and you can also use any S3-compatible disk by filling in the AWS_* variables in your .env file.

storage/logs/ holds the files of the daily log channel. New applications log to standard error and also write warnings and errors to a file for each day here.

The tests directory

The tests directory holds your tests. They live in tests/feature, in the feature_test package, so they import your application's packages by path, as your other packages do, such as app/models and app/tests. Your application starts with two: welcome_test.go tests the welcome page, and api_test.go tests the /api/version route. Run them with go test ./.... They need a PostgreSQL database, as Refreshing the database explains.

Your application's packages

Most of your code lives in Go packages at the root of your application. Each package holds one kind of code, such as routes for your route files and models for your models.

Configuring your application

bootstrap/app.go configures your application. Its Options function names your route files, your views, your database drivers, and your services, and it adds session authentication and the Inertia middleware to your web routes. It also collects the lists that your other packages keep, such as migrations.All and commands.All. You'll come back to it whenever you change the default middleware or customize error handling.

main.go runs that configuration with laravel.Run(bootstrap.Options()). With no arguments, it serves your application, and with a command name, such as laravel migrate, it runs that console command.

tests/tests.go starts from the same Options function, so your tests run the application you ship. Its New function applies your test environment overrides with laraveltest.Env and adds laraveltest.RefreshDatabase, which gives each test its own database. Your tests call tests.New(t), so add test-only overrides and options there, as Configuring your tests describes.

Routes, handlers, and middleware

The routes directory holds your route files. Most routes belong in web.go, whose routes run with sessions, encrypted cookies, CSRF protection, and Inertia. Routes in api.go are stateless, and Laravel prefixes their URIs with /api, so the /version route in api.go responds at /api/version. channels.go decides who may subscribe to your private and presence broadcast channels. To learn more, check out the routing guide's section on the default route files.

Routes point at handlers, the functions in handlers that turn a request into a response. Give each resource its own package, such as handlers/posts for the routes that list, show, and store posts, so your route file reads posts.Index and posts.Store. Your application starts with the welcome package, whose Show handler renders the welcome page.

Your application has no package for middleware. Put each middleware in the package of the feature it belongs to, such as billing.Subscribed in app/billing. A package of your own named middleware would share its name with Laravel's middleware package, so every file that used both would need an import alias. The share function in bootstrap/app.go shares data with every page, such as the application's name and flashed status messages.

Models

The models directory holds your models. A model is a struct that reads and writes one of your database tables. Your application starts with a User model in models/user.go for its users table, which authentication uses to log users in.

Services and settings

Most settings come from your .env file, so a new application has no configuration files. When you need to change something environment variables can't express, such as adding a storage disk, you add a Configure function to your options in bootstrap/app.go, as Changing Laravel's configuration describes.

Your handlers and jobs often share a service that you build once from your own settings, such as a payment provider's client. services/services.go holds them: the Services struct, the New function that builds it, and the From function that returns it from a context. The struct starts empty. For example, to add a billing client, add a field for it and set the field in New:

1package services
2 
3import (
4 "context"
5 
6 laravel "go.laravel.com"
7 "go.laravel.com/config"
8 
9 "app/billing"
10)
11 
12type Services struct {
13 Billing *billing.Client
14}
15 
16func New(env *config.Env, app *laravel.App) Services {
17 return Services{
18 Billing: billing.NewClient(
19 env.String("STRIPE_KEY", ""),
20 env.String("BILLING_CURRENCY", "usd"),
21 ),
22 }
23}
24 
25func From(ctx context.Context) *Services {
26 return laravel.ServicesOf[*Services](ctx)
27}
1package services
2 
3import (
4 "context"
5 
6 laravel "go.laravel.com"
7 "go.laravel.com/config"
8 
9 "app/billing"
10)
11 
12type Services struct {
13 Billing *billing.Client
14}
15 
16func New(env *config.Env, app *laravel.App) Services {
17 return Services{
18 Billing: billing.NewClient(
19 env.String("STRIPE_KEY", ""),
20 env.String("BILLING_CURRENCY", "usd"),
21 ),
22 }
23}
24 
25func From(ctx context.Context) *Services {
26 return laravel.ServicesOf[*Services](ctx)
27}

Here app/billing stands for a package of your own. Laravel calls New once, while it builds your application, and a handler reaches the client with services.From(r).Billing. Keep services/services.go focused on wiring your dependencies, and place each integration in its own package rather than putting all implementations in services/. To learn more about organizing services, avoiding import cycles, and keeping stateless domain logic in plain functions, check out the services guide.

services is a package of its own because bootstrap imports your handlers and jobs. If a job imported bootstrap to reach a service, Go would reject the import cycle.

Every request shares the services that New built, so each service must be safe to use from several goroutines at once. Pass per-request values, such as the logged-in user, to a service's methods as arguments rather than storing them in its fields.

Console commands and the schedule

The commands directory holds your console commands. commands.go lists them in All, and schedule.go holds your schedule, which starts with an example that runs inspire every hour.

Your application starts with an example inspire command in inspire.go, which prints an inspiring quote when you run laravel inspire. It's there to show you a command's shape, so you may delete it once you've written your own. Remove &Inspire{} from All in commands.go and its entry in schedule.go too.

Events and listeners

The events directory holds your events. An event is a struct that describes something your application did, such as OrderPlaced. The listeners directory holds the functions that respond to them, and listeners.go lists them in All. For example, a SendOrderConfirmation listener might email each customer a receipt. Your application starts with an ExampleEvent in events/example.go and a HandleExample listener in listeners/example.go, which you may delete once you've written your own.

Jobs

The jobs directory holds your queued jobs, and jobs.go lists a value of each job type in All. The list starts empty, and laravel make:job adds each job it writes to jobs.All.

Adding your own packages

As your application grows, you'll write code your application has no package for yet. Give each kind its own package. These are the usual ones:

Package What it holds
features/ Your feature flags.
mails/ Your mailables.
notifications/ Your notifications.
policies/ Your authorization policies.

Name each package for what it provides, such as billing with a billing.Charge function, not for the kind of code it holds, such as util. Put them at the root rather than in an internal directory, because no other module imports your application.

Laravel doesn't look through your directories, so it runs only the code that bootstrap.Options lists. A mailable or a policy needs no listing, because your own code calls it. A job, a listener, a command, or a feature flag does, even when it lives in a package of its own. This FulfillOrder job lives in your store package, so jobs.All lists it from there:

1// jobs/jobs.go
2package jobs
3 
4import "app/store"
5 
6var All = []any{
7 store.FulfillOrder{},
8}
1// jobs/jobs.go
2package jobs
3 
4import "app/store"
5 
6var All = []any{
7 store.FulfillOrder{},
8}

If you forget to list a job, dispatching it returns an error that tells you to add it. If you forget to list a listener, though, Laravel never calls the listener, and nothing warns you.