22 / 35

Meet Laravel Rust· 9 min read· Lesson 22 of 35

A tour of the project structure

Part 2 · Laravel Rust. Walk through every folder of a new Laravel Rust app (routes, handlers, models, migrations and more) and learn where café code belongs.

Open your new rusty-bean project and you’ll see a lot of files. Don’t panic. A Laravel app is like a well-organized café kitchen: cups on one shelf, beans in another, recipes pinned by the machine. Once you know the layout, you always know where to reach.

This lesson walks through each important folder, using the official directory structure guide (opens in new tab) as our map. You don’t need to memorize it. Come back whenever you wonder where something goes.

The big picture

Here’s a simplified view of a new app. We’ve left out some files to keep it readable:

Output
rusty-bean/
├── Cargo.toml # Rust package: name, dependencies, features
├── build.rs # rebuilds the app when you add a migration
├── .env # your local settings and secrets (not in Git)
├── package.json # front-end dependencies and npm scripts
├── vite.config.ts # front-end build settings
├── AGENTS.md # instructions for AI coding agents
├── CLAUDE.md # points to AGENTS.md
├── .agents/skills/ # focused guides for AI agents
├── database/
│ ├── migrations/ # one Rust file per database change
│ └── seeders/ # code that fills the database with rows
├── resources/ # front-end source: pages, styles, views
├── public/ # files served as-is (favicon, built assets)
├── storage/ # files the app writes while running
├── tests/ # your tests
└── src/ # your Rust code (most of your time is here)

Some folders appear only after you’ve built or run the app, and Git ignores them: target/ (Cargo’s build output), node_modules/ (front-end packages), public/build/ (assets Vite builds) and bootstrap/ssr/.

The root files

A few files at the top level are worth knowing:

  • Cargo.toml is the same file you met in Hello world with Cargo. It holds your project’s name, its dependencies, and Laravel’s Cargo features, optional parts you switch on, such as database-postgres or database-sqlite.
  • build.rs is a small Rust program Cargo runs before compiling. Laravel compiles your migrations into the app, so this script rebuilds the app when you add one.
  • .env holds settings that differ between your laptop and the live server: database address, secret key, and so on.
  • package.json and vite.config.ts belong to the front end. They list JavaScript packages and how to build them.

Tip

You can rename your project in the [package] section of Cargo.toml. Keep the [lib] name as app, though: Laravel and your tests find your code by that name, as in use app::models::User.

The src folder: your Rust code

This is where you’ll spend most of your time. It’s split into modules, one per kind of code. That’s the module system from Modules and crates at work.

The four starter files

File What it does
src/main.rs The program’s entry point. Without arguments it serves your app. With a command name, such as laravel migrate, it runs that command.
src/app.rs Configures your app: registers route files, middleware, migrations, the login system and more.
src/lib.rs Declares your app’s modules (pub mod models; and so on).
src/testing.rs Starts the same app for your tests, so you test what you ship.

src/app.rs is where route files get plugged in. Here’s the routing part, straight from the official docs:

src/app.rs
.with_routing(|routing, _| {
routing
.web(routes::web::routes)
.api(routes::api::routes)
.health("/up");
})

Step by step:

  1. .with_routing(...) takes a closure, the same kind you wrote in Closures and iterators. Laravel calls it at startup and gives it a routing builder.
  2. .web(routes::web::routes) registers the function routes from src/routes/web.rs as your web routes.
  3. .api(routes::api::routes) registers src/routes/api.rs as your API routes.
  4. .health("/up") adds the health-check route you visited in the last lesson.

Notice that routes::web::routes is a function path, not a string. You’ll see this pattern all over Laravel Rust.

src/routes/: the front door

Route files decide which code runs for which URL:

  • web.rs is for pages people visit in a browser, like the café’s menu and order form. These routes get sessions, encrypted cookies and CSRF protection (a guard that stops other websites from submitting forms on your visitors’ behalf).
  • api.rs is for machines, like a mobile app checking order status. These routes are stateless (no sessions), and Laravel adds /api to the front of each one. A /version route in api.rs answers at /api/version.
  • channels.rs decides who may listen to live broadcast channels. You’ll use it in Mail, broadcasting and Inertia.

src/http/: handlers and middleware

  • src/http/handlers/ holds your handlers, the async functions that turn a request into a response. The docs recommend one module per resource. For the café that means handlers::menu for menu pages and handlers::orders for orders.
  • src/http/middleware/ holds middleware, code that runs before or after handlers, such as “only logged-in staff past this point”. It includes HandleInertiaRequests and an example middleware you can replace.

src/models/: your data

Each model is a struct that reads and writes one database table. A new app ships with a User model for the users table, which the login system uses. You’ll add MenuItem and Order in Models and relationships.

Other src folders

  • src/config/ holds config files. Most settings come from .env, so a new app has only three: database.rs, cors.rs and services.rs.
  • src/console/ holds console commands and the schedule (jobs that run on a timer). There’s an example inspire command. Try laravel inspire.
  • src/events/ and src/listeners/ hold events (things that happened, like “order placed”) and the functions that react to them.

database/: tables and sample data

  • database/migrations/ has one Rust file per database change, such as “create the menu_items table”. The file names start with a date and time so they run in order. These files compile into your app, so a server can run migrate without your source code.
  • database/seeders/ holds seeders, code that fills the database with starter rows. It’s handy for loading a sample café menu.

The front-end folders

  • resources/js/pages/ holds your Inertia page components. A handler that calls inertia::render("Welcome", ...) shows Welcome.tsx from this folder.
  • resources/views/ holds server-side HTML templates, including app.html, the page every Inertia page renders inside.
  • resources/css/app.css holds your styles (they import Tailwind).
  • public/ holds files sent to browsers as-is, such as favicon.ico.

storage/ and tests/

  • storage/app/ is for files your app keeps, such as uploads or a SQLite database file. Git ignores what’s inside.
  • tests/ holds your tests. A new app comes with welcome.rs and api.rs. Run them with cargo test (they need a database). You’ll write your own in Testing your app.

Adding your own modules

As the café grows, you’ll add folders that don’t exist yet. The docs suggest these names:

Module Holds
src/jobs/ Queued background jobs
src/mail/ Emails (mailables), such as receipts
src/policies/ Rules about who may do what
src/services/ Your own services, such as a payment client

There’s one important rule: Rust only compiles modules you declare. Adding a file isn’t enough. You must also add a mod line. Here’s the docs’ pattern, adapted for a future receipt job:

src/lib.rs
pub mod jobs;
src/jobs/mod.rs
mod send_receipt;
pub use send_receipt::SendReceipt;
  1. pub mod jobs; in src/lib.rs tells Rust that a jobs module exists, in src/jobs/mod.rs.
  2. mod send_receipt; in src/jobs/mod.rs declares the file src/jobs/send_receipt.rs.
  3. pub use ... re-exports the job, so other code can write crate::jobs::SendReceipt.

Warning

If you forget the mod line, Rust skips the file without any warning, and nothing in it runs. Laravel finds jobs, commands and listeners by their attributes, but only in modules Rust has compiled. If new code “does nothing”, check your mod lines first.

Try it yourself

  1. Where would you put each of these café pieces? (a) The function that shows the menu page. (b) The rule that /orders/{order} goes to that order’s page. (c) A migration that creates an orders table. (d) A React component for the order form.
  2. You create src/http/handlers/menu.rs, but your route file says the menu module can’t be found. What did you forget?
Show solution
  1. (a) A handler in src/http/handlers/menu.rs. (b) A route in src/routes/web.rs. (c) A new file in database/migrations/. (d) A page component in resources/js/pages/.
  2. You need to declare the module. Add pub mod menu; to src/http/handlers/mod.rs. Until you do, Rust doesn’t compile the file at all.

Recap

  • src/ holds your Rust code: routes/ for URLs, http/handlers/ for request handling, models/ for data.
  • src/app.rs plugs everything together, including which route files to use.
  • web.rs routes get sessions and CSRF protection. api.rs routes are stateless and prefixed with /api.
  • database/migrations/ holds one Rust file per database change, compiled into your app.
  • New files only count once you declare them with mod. Rust silently skips undeclared files.

What’s next

You know where routes and handlers live. Now let’s write some! In Routing and handlers you’ll build the café’s first real pages.

0 of 35 lessons complete

Search lessons

Type to search all lessons.