Request Lifecycle
Introduction
You feel more confident with a tool when you understand how it works. This guide gives you a high-level tour of a Laravel application: how it starts, what happens to a request on its way to your handler, and how the response gets back to the browser. Along the way, you'll see where to register services, add middleware, and handle errors. If some of the terms are new, don't worry. Each one links to the guide that covers it, and a rough picture of the whole is enough to start.
Starting up
Creating the application
You describe your application in src/app.rs, where the configure function calls Application::configure(). The builder collects your providers, routes, middleware, and other settings, but nothing runs yet. The main function in src/main.rs calls run on the builder:
1use std::process::ExitCode;23fn main() -> ExitCode {4 app::configure().run()5}1use std::process::ExitCode;23fn main() -> ExitCode {4 app::configure().run()5}
Your tests build the same application through app::testing::create in src/testing.rs.
Whether your application is serving requests or running a test, Laravel puts it together in this order:
- Configuration. Laravel reads your environment variables and
.envfile. Service providers set their defaults, then your configuration files run, so your values override a package's. - Providers register. Each package's provider registers its services and adds its routes, middleware, commands, and other definitions.
- Your application registers. Laravel finds your
#[singleton]and#[bind]services, listeners, and commands. Then yourwith_services,with_routing,with_middleware, and other callbacks insrc/app.rsrun, after every provider. - Laravel checks the result. A mistake such as an invalid environment variable, two routes that conflict, or a task scheduled for a command that doesn't exist stops the application.
Booting
Next, the application boots. Every provider's boot method runs, followed by the booted callbacks in src/app.rs. The application boots once, before it serves its first request or runs a command, and every request and command after that shares its services. If a boot hook returns an error, the application stops before it handles anything.
Booting doesn't build your services. Laravel builds each one the first time something asks for it, such as a handler's Inject<T> argument, and shares it from then on.
Once the application boots, run serves it, on 127.0.0.1:8000 by default. When you name a console command, such as laravel route:list, run runs that command instead, with the same configuration and services.
Handling a request
The kernel
Every request first enters Laravel's HTTP kernel. To the server, the kernel is your whole application: it receives a request and returns a response. The kernel removes forwarded headers unless the request came through a trusted proxy. It gives the request a request ID for your logs, and a context, which helpers such as laravel::config() and laravel::url::route read.
Global middleware and routing
Next, the request passes through the global middleware, which run on every request. They reject hosts your application doesn't serve, answer CORS requests, and trim the strings in form input. They run before routing, so method spoofing can turn an HTML form's POST into the PUT or DELETE your route expects.
Then the router finds the route that matches the request. If no route matches, Laravel serves the matching file from public/. If no file matches either, your fallback route handles the request, or the response is a 404.
The route
The matched route runs its middleware, then its handler:
- The route file's middleware group. Routes in
src/routes/web.rsrun the middleware in thewebmiddleware group, which decrypt cookies, start the session, and check the CSRF token. Your application also addsHandleInertiaRequeststo thewebgroup, which shares data with your Inertia pages. Routes insrc/routes/api.rsrun theapimiddleware group instead, which has no middleware until you add some, so API routes have no session or cookies. - The route's own middleware. These are the middleware you assign to the route or to a route group that contains it, such as
Authenticate. - The handler. It does your application's work and returns a response.
Sending the response
The response travels back out through the route's middleware in reverse order, so each one may inspect or change it. When a handler returns an error or panics, Laravel renders the error as a response first. The route's middleware then see an ordinary response, such as a 404 or 500 page, the same as when the handler succeeds.
If a middleware fails or panics instead, the kernel turns that into an error response too. One failing request never takes down the server. Finally, the kernel logs the request with its status and duration and sends the response.
Where your startup code goes
Packages take part in each step above through a service provider, which adds their services, routes, middleware, and commands, and can check its setup when the application boots. Your application doesn't need a provider of its own, because src/app.rs has the same hooks, such as with_services and booted, and they always run after every package's provider. Put your application's own startup code there.
If you write a Laravel Rust package that you want to publish and share with the community, you'll use a service provider to set its configuration defaults, add its routes, middleware, and commands, and do anything else your application would do in src/app.rs. The package development guide shows you how, and lists each builder with its src/app.rs counterpart.