Disable error reporting entirely in Laravel production?

Disable error reporting entirely in Laravel production?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Disable error reporting entirely in Laravel production?

When you are maintaining legacy code within a modern framework, the friction between ErrorException and your desire to ship features quickly is palpable. You have old scripts that technically work but throw warnings for undefined variables, such as $var or script_name_vars_def. While setting APP_DEBUG=false and APP_LOG_LEVEL=emergency are standard operating procedures, they often fail to silence the noise entirely in production environments.

The core issue stems from how Laravel handles errors. Even with debugging disabled, the framework defaults to throwing exceptions for uncaught PHP errors. Simply calling set_error_handler(null) or display_errors(false) inside your application code is insufficient because these functions do not prevent the ErrorException class from being instantiated and thrown by the framework's error handler.

To truly silence these warnings while retaining Laravel's routing benefits, you must intercept the request lifecycle before the exception can be rendered. The most robust approach for this scenario is implementing a middleware that suppresses errors globally for specific routes or applications.

Implementing Global Error Suppression

The strategy involves creating a middleware that sets error_reporting to zero and potentially resets the error handler, effectively telling PHP to ignore warnings during the execution of your legacy logic. You can generate this middleware using Artisan:

php artisan make:middleware SuppressExceptions

Inside the generated file (app/Http/Middleware/SuppressExceptions.php), you should modify the handle method. This is where you execute the suppression logic:

public function handle($request, Closure $next)
{
    // Suppress all errors (E_ALL & ~E_DEPRECATED & ~E_USER_DEPRECATED is safer)
    error_reporting(0);
    
    // Optional: Disable display of errors to ensure nothing leaks to browser
    ini_set('display_errors', 0);
    
    // Proceed with the request
    return $next($request);
}

This middleware acts as a shield. By setting error_reporting(0), you instruct PHP not to generate error notices for undefined variables or other runtime warnings. This allows your legacy code, like the example requiring /old_index.php inside an ob_start() block, to execute without triggering the standard Laravel exception flow.

Registering the Middleware

Once the middleware is created and configured, it must be registered in the application's HTTP kernel. Open app/Http/Kernel.php and add the class to your middleware groups:

protected $middlewareGroups = [
    'web' => [
        \App\Http\Middleware\SuppressExceptions::class,
        // ... other web middleware
    ],
];

By placing this in the web middleware group, it applies to all routes that pass through the standard web stack. This is ideal for your specific use case where you are wrapping legacy scripts in new Laravel controllers. The middleware runs before the controller logic executes, ensuring that any warnings generated by the included old code are swallowed silently.

Trade-offs and Considerations

While this approach solves the immediate problem of "shut up and work," there are significant caveats to consider. Silencing all errors effectively turns your application into a black box. If a critical failure occurs—such as a division by zero or a missing required file—you will not see an error message in the browser or potentially in the logs unless configured otherwise.

In a production environment, relying on error_reporting(0) is generally discouraged for new applications because it masks bugs rather than fixing them. However, for legacy code migration strategies where stability is prioritized over immediate debugging of known issues, this is an acceptable temporary state. You must ensure your logging infrastructure (Laravel Log) is still capturing stack traces if you are using log_channel configurations that might bypass the standard exception handling, or at least verify that your APP_LOG_LEVEL is set to capture whatever data remains accessible.

Ultimately, this middleware provides a clean separation between your modern routing layer and the legacy code logic, allowing you to fix the undefined variable issues later without halting development today.

Our network