Laravel bootstraps the application for every HTTP request that comes into your server. This means that each time a user makes a request to your Laravel application, the entire application is bootstrapped, including the loading of service providers, configuration files, and routes.
The require_once in public/index.php does not ensure that the application is only bootstrapped once across multiple requests. Instead, it ensures that during the handling of a single request, the bootstrap/app.php file is only included once to prevent re-declaration errors.
Here's a simplified version of what happens during each request to a Laravel application:
- The web server (e.g., Apache or Nginx) receives the HTTP request and directs it to the
public/index.php file of your Laravel application.
- The
index.php file includes the bootstrap/app.php file, which creates the application instance.
- Service providers are registered and booted, configuration files are loaded, and the application is set up.
- The request is captured and handed to the application instance.
- The application routes the request to the appropriate controller and action.
- The response is generated and sent back to the user.
Here's the relevant part of the public/index.php file:
$app = require_once __DIR__.'/../bootstrap/app.php';
$request = Illuminate\Http\Request::capture();
$response = $app->handle($request);
$response->send();
$app->terminate($request, $response);
Each time a new HTTP request is made, this process starts from the beginning. Laravel does not keep the application in memory between requests in a standard PHP web server setup. This is different from how some application servers in other languages or frameworks might work, where the application is loaded into memory once and then persists across multiple requests.
If you want to reduce the cost of bootstrapping the application for each request, you could look into using PHP's opcache, which can cache the compiled PHP scripts in memory, or consider using Laravel Octane, which is designed to keep the application bootstrapped between requests by running on servers like Swoole or RoadRunner. However, these approaches have their own considerations and are not part of the standard Laravel setup.