Defining Routes & Controllers

This example shows how to wire routes to controllers using Base's Router and Controller.

Directory Layout

public/
  index.php
app/
  controllers/
    HelloController.php

/public/index.php:

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

use Stilmark\Base\Env;
use Stilmark\Base\Router;

// Start session if you use Auth/session
session_start();

// Load environment variables
Env::load(__DIR__ . '/../.env');

// Dispatch all routes (Router internally uses FastRoute)
$router = new Router();
$router->dispatch();

Defining Routes (inside Router's route map)

The Router internally configures FastRoute\simpleDispatcher with your application's routes. Adjust the route callback in Router to include your routes, e.g.:

Handler strings are resolved as "Namespace\\Class@method".> Route parameters (e.g. {name}) are bound to controller method arguments.

/app/controllers/HelloController.php:

Test with curl

Last updated