Skip to main content

Modern Routing

The Router class provides a clean, Laravel-style syntax for defining routes with automatic PSR-7 request injection. This approach is ideal for new projects or when centralizing routes in a single file.

When to Use Modern Routing

  • New Projects - Starting fresh with modern patterns
  • Route Files - Centralizing routes in routes/web.php or routes/api.php
  • Cleaner Syntax - Prefer Router::get() over route(method(GET), url_path())
  • Automatic Parameter Detection - No need for separate url_path_params()
  • PSR-7 Request Injection - Handlers receive a ServerRequest automatically

Basic Usage

<?php
require_once 'vendor/autoload.php';

use PhpCompatible\Router\Router;

Router::run(function() {
Router::get('/', function() {
return 'Home';
});

Router::get('/about', function() {
return 'About';
});

// Automatic request injection when handler accepts a parameter
Router::get('/api/users/:id', function($request) {
$id = $request->getParam('id');
return array('user_id' => $id); // Auto-converted to JSON
});
});

Route Files Structure

/app
├── routes/
│ ├── web.php # Web routes
│ └── api.php # API routes
├── controllers/
│ └── UserController.php
└── public/
└── index.php # Entry point

public/index.php:

<?php
require_once __DIR__ . '/../vendor/autoload.php';

use PhpCompatible\Router\Router;

Router::run(function() {
require __DIR__ . '/../routes/web.php';
require __DIR__ . '/../routes/api.php';
});

Topics