search ESC

Routing

Launchpad\Core\Routing\Router is a minimal HTTP router that supports verb methods, path parameters, groups, middleware, and attribute-based discovery. Routes are loaded from files at boot time by Application::boot().

Basic usage

Routes are registered in routes/web.php (or any file passed to Router::loadFile()). The $router variable is injected into scope when the file is loaded.

use App\Controllers\HomeController;
/** @var \Launchpad\Core\Routing\Router $router */

$router->get('/',              [HomeController::class, 'index']);
$router->get('/docs/{topic}',  [HomeController::class, 'topic']);
$router->post('/contact',      [HomeController::class, 'submit']);

HTTP verbs

Every standard verb has a dedicated method:

get() post() put() patch() delete() any()

any() matches any method and is useful for catch-all fallbacks.

Path parameters

Wrap the parameter name in curly braces. The router extracts it into the Request object, accessible via $request->param(...):

$router->get('/users/{id}', [UserController::class, 'show']);

// Inside the controller:
public function show(Request $request): Response
{
    $id = (int) $request->param('id');
    // ...
}

Groups

Use group() to share prefixes and middleware across related routes:

$router->group(['prefix' => '/api/v1', 'middleware' => ['auth']], function ($router) {
    $router->get('/me',        [UserController::class, 'me']);
    $router->get('/posts',     [PostController::class, 'index']);
    $router->post('/posts',    [PostController::class, 'store']);
});

Resource routes

resource() registers the five canonical CRUD routes for a controller in one call:

$router->resource('/posts', PostController::class);
// Equivalent to:
//   GET    /posts         → index
//   POST   /posts         → store
//   GET    /posts/{id}    → show
//   PUT    /posts/{id}    → update
//   DELETE /posts/{id}    → destroy

Loading route files

Important: Router::loadFile() silently skips missing files. This is deliberate — it lets consumer projects omit route files they don't need (e.g. no api.php) without crashing the boot sequence.

Related