Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon Can you explain the role of the sandbox instance in Octane
    Laravel Service Provider


Can you explain the role of the sandbox instance in Octane


In Laravel Octane, the sandbox instance plays a crucial role in ensuring that each incoming request is handled with a clean and isolated environment. Here are the key aspects of the sandbox instance:

Cloning the Application Instance

1. Sandbox Creation: When a request comes in, Octane clones the original application instance to create a sandbox instance. This ensures that each request has its own isolated environment, preventing state persistence issues between requests.

Handling Requests

1. Request Handling: The sandbox instance is used to handle the incoming request. This includes resolving dependencies, executing middleware, and dispatching events.

Managing State

1. Config Service: The sandbox instance includes a separate config service that is reset for each request. This ensures that any changes to the configuration are isolated to the current request.
2. Cache and Session: The sandbox instance flushes the cache and session state to prevent any data from persisting across requests.

Event Listeners

1. RequestReceived Event: Octane listens to the `RequestReceived` event to prepare the sandbox instance for handling the request. This includes setting the application instance, flushing caches and sessions, and setting locales.

Service Providers

1. Singletons: When registering singletons, Octane ensures that the sandbox instance is passed to the singleton's constructor. This ensures that the singleton uses the correct instance of the application.

Best Practices

1. Avoid Passing Application Instance: Avoid passing the application instance directly to services or singletons. Instead, use the `app()` helper or `Container::getInstance()` to ensure that the correct instance is used.
2. Use Callbacks: Use callbacks to return the sandbox instance instead of passing the application instance directly.

Example Configuration

php
// Instead of passing the application instance directly
$this->app->singleton(Service::class, function ($app) {
    return new Service($app);
});

// Use a callback to return the sandbox instance
$this->app->singleton(Service::class, function ($app) {
    return new Service(fn () => Container::getInstance());
});

Example Event Listener

php
// Dispatch an event before handling a request
$server->on('request', function ($request) use ($app) {
    $sandbox = clone $app;
    Container::setInstance($sandbox);
    $sandbox->make('events')->dispatch(new RequestReceived);
    // Handle the request
});

By using the sandbox instance effectively, you can ensure that your application runs efficiently and securely with Octane.

Citations:
[1] https://www.getoctane.io/sandbox
[2] https://otoy1473900221.zendesk.com/hc/en-us/articles/115001577606-Instances
[3] https://blog.octanesolutions.com.au/sandbox-analysis.delivered
[4] https://github.com/laravel/octane/issues/241
[5] https://divinglaravel.com/laravel-octane-bootstrapping-the-application-and-handling-requests