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:
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.tomlis 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 asdatabase-postgresordatabase-sqlite.build.rsis 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..envholds settings that differ between your laptop and the live server: database address, secret key, and so on.package.jsonandvite.config.tsbelong 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:
.with_routing(|routing, _| { routing .web(routes::web::routes) .api(routes::api::routes) .health("/up");})Step by step:
.with_routing(...)takes a closure, the same kind you wrote in Closures and iterators. Laravel calls it at startup and gives it aroutingbuilder..web(routes::web::routes)registers the functionroutesfromsrc/routes/web.rsas your web routes..api(routes::api::routes)registerssrc/routes/api.rsas your API routes..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.rsis 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.rsis for machines, like a mobile app checking order status. These routes are stateless (no sessions), and Laravel adds/apito the front of each one. A/versionroute inapi.rsanswers at/api/version.channels.rsdecides 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 meanshandlers::menufor menu pages andhandlers::ordersfor orders.src/http/middleware/holds middleware, code that runs before or after handlers, such as “only logged-in staff past this point”. It includesHandleInertiaRequestsand 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.rsandservices.rs.src/console/holds console commands and the schedule (jobs that run on a timer). There’s an exampleinspirecommand. Trylaravel inspire.src/events/andsrc/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 themenu_itemstable”. The file names start with a date and time so they run in order. These files compile into your app, so a server can runmigratewithout 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 callsinertia::render("Welcome", ...)showsWelcome.tsxfrom this folder.resources/views/holds server-side HTML templates, includingapp.html, the page every Inertia page renders inside.resources/css/app.cssholds your styles (they import Tailwind).public/holds files sent to browsers as-is, such asfavicon.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 withwelcome.rsandapi.rs. Run them withcargo 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:
pub mod jobs;mod send_receipt;
pub use send_receipt::SendReceipt;pub mod jobs;insrc/lib.rstells Rust that ajobsmodule exists, insrc/jobs/mod.rs.mod send_receipt;insrc/jobs/mod.rsdeclares the filesrc/jobs/send_receipt.rs.pub use ...re-exports the job, so other code can writecrate::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
- 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 anorderstable. (d) A React component for the order form. - You create
src/http/handlers/menu.rs, but your route file says themenumodule can’t be found. What did you forget?
Show solution
- (a) A handler in
src/http/handlers/menu.rs. (b) A route insrc/routes/web.rs. (c) A new file indatabase/migrations/. (d) A page component inresources/js/pages/. - You need to declare the module. Add
pub mod menu;tosrc/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.rsplugs everything together, including which route files to use.web.rsroutes get sessions and CSRF protection.api.rsroutes 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