Luminova Framework

PHP Luminova: Implementation of Custom Error View Controllers for Pages

Last updated: 2024-09-15 21:46:09

Customize or add new error handlers for different routing contexts. By default, it provides handlers for the web or API contexts, which you can modify or customize as needed.

The ViewErrors class allows you define and manage error-handling methods to customize how view errors are displayed. Error-handling methods for 404 error type that may occur during the execution of your application routing or view rendering. This provide a way to display meaningful error messages to users.


NOTE:

  • Error handler methods can be either static or non-static.
  • Methods must be declared as public, not protected or private.
  • Dependency injection is enabled by default for error handlers, allowing you to pass any class you need as a parameter in the method signature.


Methods

onWebError

The default error handler for the web routing context.

public static onWebError(Application $app): void

Parameters:

ParameterTypeDescription
$app\App\ApplicationApplication instance available

onApiError

The default error handler for the API routing context.

public static onApiError(Application $app): void

Parameters:

ParameterTypeDescription
$app\App\ApplicationApplication instance available

Usage Example

To register your error handler, it must be callable or pass an array where the first element of the array is your error handler class name and the second element is the method name.

// public/index.php
<?php
use \Luminova\Routing\Prefix;
use App\Controllers\Http\Errors\ViewErrors;

$app->router->context( 
    new Prefix('foo', [ViewErrors::class, 'onFooError']),
    //...
);

This example demonstrates how to register an error handler in your application's routing context. The onFooError method from the ViewErrors class will handle errors for the FOO context.