Using the Router

The HexPHP router maps URLs precisely to Controller actions. It is fast, explicit, and easy to maintain.

Defining Routes

Routes are registered inside config/routes.php. You map HTTP methods and precise URIs to arrays representing the Controller and Method.

<?php
use App\Controllers\Router;

// A simple GET request map
Router::get('/', ['HomeController', 'index']);

// A POST request handler
Router::post('/submit-form', ['FormController', 'store']);

// Documentation routes
Router::get('/docs', ['DocsController', 'index']);
Router::get('/docs/router', ['DocsController', 'router']);

Middleware Support

You can easily attach middleware to intercept requests before they reach your controller by passing a third standard array argument containing the class names.

// Adding the Auth middleware to protect a route
Router::get('/dashboard', ['DashboardController', 'index'], ['Auth']);

// Chaining multiple middlewares
Router::post('/update-profile', ['ProfileController', 'update'], ['Auth', 'VerifyCsrfToken']);

Middleware logic lives inside the app/Middleware/ directory. A middleware simply defines a public function handle() where you can redirect, invoke `Router::abort(403)`, or let the request pass.

404 Handling

If the requested URI does not explicitly match any entries in the routing arrays for that HTTP method, the router automatically triggers:

Router::abort(404, "404 - Page Not Found")

Adding a New Route Example

  1. Create app/Controllers/AboutController.php with a method called show().
  2. Create your UI components in app/Components/about/.
  3. Register Router::get('/about', ['AboutController', 'show']); in your routes file.