Luminova Framework

PHP Luminova: Error Attributes for HTTP Controllers

Last updated: 2026-11-28 20:57:12

Describe routing error callbacks on an HTTP controller with the Error attribute.

The repeatable Error attribute describes an HTTP routing error handler using a context name, URI pattern, and callback. For a prefix-scoped fallback, use the Prefix attribute’s error handler.


Examples

Single Handler

This declaration associates the web context and root pattern with the onWebError callback.

// app/controllers/Http/WebController.php

namespace App\Controllers\Http;

use Luminova\Base\Http\AbstractController;
use Luminova\Attributes\Route;
use Luminova\Attributes\Error;
use App\Errors\Controllers\AppError;

#[Error('web', '/(:root)', [AppError::class, 'onWebError'])]
class WebController extends AbstractController
{
   #[Route('/', methods: ['GET'])]
   public function index(): int
   {
      return $this->view('index');
   }
}

Multiple Handlers

Multiple declarations can supply separate patterns and callbacks.

// /app/controllers/Http/WebController.php

namespace App\Controllers\Http;

use Luminova\Base\Http\AbstractController;
use Luminova\Attributes\Route;
use Luminova\Attributes\Error;
use App\Errors\Controllers\AppError;

#[Error('admin', '/(?!dashboard/).*', [AppError::class, 'onWebError'])]
#[Error('admin', '/dashboard/(:root)', [AppError::class, 'onDashboardError'])]
class WebController extends AbstractController
{
   #[Route('/admin', methods: ['GET'])]
   public function home(): int
   {
      return $this->view('admin/index');
   }

   #[Route('/admin/dashboard', methods: ['GET'])]
   public function dashboard(): int
   {
      return $this->view('admin/dashboard');
   }
}

In the above example:

  • The first Error attribute '/ (?!dashboard/).*' matches all routes except those that begin with /dashboard/ This utilizes a negative lookahead assertion (?!dashboard/) to exclude any URL that starts with /dashboard/.
  • The second Error attribute '/dashboard/(:root)' matches any routes that start with /dashboard, including both /dashboard and /dashboard/. The (:root) placeholder allows for the capture of any additional segments after /dashboard, including the possibility of matching nothing (an empty string).

Web URI Prefixing

The prefix argument names a routing context. The separate pattern argument carries the URI match, so 'web' does not add a literal /web path segment.


Class Definition

  • Full namespace: Luminova\Attributes\Error
  • This class is final.

__construct

Construct a repeatable class attribute that describes an HTTP routing error callback.

public function __construct(
    string $prefix = 'web',
    string $pattern = '/',
    string|array|null $onError = null,
)

Parameters:

ParameterTypeDescription
$prefixstringThe URI prefix context name used to categorize URIs (default: web).
Typically the first segment of the URI (e.g., api, blog).
$patternstringThe route URI pattern to allow this error handler (default: / root).
(e.g., /blog/(:int), /blogs/{$id}, /blog/(\d+), / or /.*).
$onErrorstring|array|nullA callable string or [class, method] array. Supply a handler explicitly.

Throws:

See reference for Dynamic URI Segment Placeholders documentation.