Luminova Framework

PHP Luminova: HTTP Controller's Method Scope Route Attribute

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

Define HTTP routes, aliases, middleware, and error handlers on controller methods.

The Route Attribute in Luminova provides a clean, declarative way to define URI routes directly inside your controller methods — for both HTTP and CLI applications.Because this attribute is repeatable, you can assign multiple routes to a single method or applying it to different methods as needed.

Luminova uses PHPToken to parse controller classes and resolve the correct handler for each request. While this process could be heavy in theory, Luminova supports attribute caching for production environments, eliminating the need to re-parse attributes on every request and greatly improving performance.

For CLI command registration and middleware, see Command Attributes.


Setting up Attribute Routing

The PHP attribute routing, allowing you to define routes directly in your controller classes without creating separate route files. Follow these steps to enable and optimize it:


1. Enable Attribute Routing

Edit your .env file and set feature.route.attributes=true:

feature.route.attributes = true

2. Enable Attribute Caching

To avoid re-parsing attributes on every request (which saves time in production), enable caching:

feature.route.cache.attributes = true

The cached routes will be stored in:/writeable/caches/routes/


3. Adjust Route Context Initialization

When using attributes, you don’t need to predefine route contexts configuration.

In your public/index.php, you may currently have arguments passed to context(...) method. Using the Prefix class or array configurations.

Examples:

// /public/index.php

use Luminova\Routing\Prefix;
use App\Errors\Controllers\AppError;

Boot::http()->router->context([
   Prefix::with(Prefix::WEB, [AppError::class, 'onWebError']),
   Prefix::with(Prefix::API, [AppError::class, 'onApiError']),
   Prefix::with(Prefix::CLI),
])->run();

When routing with attributes, set the argument to null or blank.

This prevents the router from loading unnecessary method-based context files, reducing overhead and improving overall performance.

// /public/index.php

Boot::http()->router->context()->run();

Usages

HTTP Controller Class Example

The controller route handlers can be define with HTTP methods such as GET, POST, PUT, DELETE, OPTIONS, QUERY, and more.

// /app/Controllers/Http/Controller.php

namespace App\Controllers\Http;

use Luminova\Attributes\Route;
use Luminova\Base\Http\AbstractController;

class Controller extends AbstractController
{
   // Your controller methods
}

Defining Routes with Attributes

Basic HTTP Route

Defines an HTTP route that responds to GET requests at the root URL (/):

// /app/Controllers/Http/Controller.php

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

Equivalent in Method-Based:

// /routes/web.php

Luminova\Routing\Router::get(
   '/', 
   'Controller::index'
);

HTTP Route with Middleware

Defines an HTTP route that executes middleware before running the main route handler:

// /app/Controllers/Http/Controller.php

#[Route('/', methods: ['GET'], middleware: Route::BEFORE_MIDDLEWARE)]
public function middleware(): int
{
   return STATUS_SUCCESS;
}

Equivalent Router Method:

// /routes/web.php
Luminova\Routing\Router::middleware(
   'GET', 
   '/', 
   'Controller::middleware'
);

HTTP Route with Error Handling

Defines a route to handle unmatched requests or errors using the isErrorHandler flag:

// /app/Controllers/Http/Controller.php

#[Route('/(:root)', methods: ['ANY'], isErrorHandler: true)]
public function error(): int
{
   return $this->send('Page not found', status: 404);
}

Equivalent in Method-Based:

// /routes/web.php
use App\Controllers\Http\Controller;

Luminova\Routing\Router::onError(
   [Controller::class, 'error'], 
   '/(:root)'
);

Defining Multiple Routes for One Method

Sometimes you want one controller method to respond to several different URLs. With the Route attribute, there are two ways to do this:

  1. Repeat the attribute for each URL pattern
  2. Use the $aliases property in a single attribute

Recommendations:

  • Use $aliases for static paths that should behave exactly the same.For example, /, /home, /dashboard should all load the same page.

  • Use repeated attributes for dynamic routes or when each URL needs its own configuration.For example, if /home is GET only but /dashboard should allow both GET and POST.


Repeated Route Attribute

You can add the Route attribute multiple times if different URLs need to point to the same method but may have different HTTP methods or settings.

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

When to use:

  • When some URLs need different HTTP methods or other custom settings.

Using Aliases

If all the URLs behave the same, you can keep things cleaner by defining them in one attribute using the $aliases property. This avoid repeating the same attribute multiple times.

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

Note:All aliases inherit the same methods and settings as the main route (/ in this example).


Class Definition

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

Constants

NameValueDescription
BEFORE_MIDDLEWAREbeforeRun middleware before the route handler.
AFTER_MIDDLEWAREafterRun middleware after the route handler.

Properties

pattern

Defines the URL pattern that the route should match.This is how the router knows which request or command to handle.

Examples:

  • HTTP Route: '/users/([0-9]+)' — Matches URLs like https://example.com/users/123.

Methods Property

Specifies the HTTP methods that the route should respond to (e.g., GET, POST, PUT).Useful for RESTful APIs to restrict actions based on request type.

/**
 * @var string[]
 */
public array $methods = ['GET'];

Example:methods: ['GET', 'POST'] — The route only handles GET and POST requests.


isErrorHandler

Indicates whether the route is an HTTP error handler.Error routes catch and handle 404 errors for the specified HTTP methods.

Example:isErrorHandler: true — This route will handle requests that do not match any other route.


middleware

Specifies middleware to apply to the route.Middleware can execute code before or after the main route handler, useful for authentication, logging, or setup tasks.

Examples:

  • middleware: Route::BEFORE_MIDDLEWARE — Runs before the main route handler (HTTP pre-processing).
  • middleware: Route::AFTER_MIDDLEWARE runs after the HTTP route handler.

aliases

Defines alternative URI patterns that map to the same route as the main pattern.All aliases share the same HTTP methods, middleware, and error handler as the main route.This helps avoid repeating multiple Route attributes for identical behavior.

/**
 * @var string[]|null
 */
public ?array $aliases = null;

Example:

#[Route(pattern: '/', aliases: ['/home', '/default', '/dashboard'], methods: ['GET'])]

All of these URLs will respond exactly like the main route /.

Methods

__construct

Register an HTTP route, route middleware, or a method-level error handler.

public function __construct(
    string $pattern = '/',
    array $methods = ['GET'],
    bool $isErrorHandler = false,
    ?string $middleware = null,
    ?array $aliases = null,
)

Parameters:

ParameterTypeDescription
$patternstringURI pattern (default /).
$methodsstring[]Accepted HTTP methods (default ['GET']). Use ['ANY'] by itself to match all methods.
$isErrorHandlerboolRegister the method as a routing error handler when true (default false).
$middlewarestring|nullUse BEFORE_MIDDLEWARE or AFTER_MIDDLEWARE, or leave it at null for a route handler.
$aliasesstring[]|nullAdditional URI patterns for the same handler (default null).

Throws:


Attribute Commands

Clearing Cached Attributes

If attribute caching is enabled, changes or new attributes may not take effect because the cache is stored permanently. To clear the cache:

  • Manually delete cached PHP files from /writeable/caches/routes/
  • Or use the Novakit command:
php novakit context --clear-attr

This ensures that your latest attribute definitions are applied on the next request.


Performance Comparison

You can compare attributes vs router methods by enabling performance profiling:

debug.show.performance.profiling = true

Metrics such as:

  • Execution Time
  • Memory Usage
  • Files Loaded

will be displayed, helping you make an informed decision about which method to use in production.