Documentation

Routing

New Routing System (Preferred)

Starting from the latest Boostack version, routing is handled through an internal Router class instead of Apache .htaccess rules. This approach is more flexible, testable, and maintainable. All requests should be directed to the index.php file, and routes should be configured via the initRoutes() function.

Setup

Create the following structure in your initRoutes() function to register your application's routes:

use Boostack\Core\Routing\Router;
use Boostack\Core\Routing\HttpMethod;

function initRoutes(): Router {
    $router = new Router();

    //add your Routes here...
    // e.g:
    // $router->addRoute('', [My\Controllers\Index::class, 'init']);
    // $router->addRoute('login', [My\Controllers\Login::class, 'init'], HttpMethod::GET);
    // $router->addRoute('login', [My\Controllers\Login::class, 'init'], HttpMethod::POST);
    // $router->addRoute('setup', "setup/index.php");
    // $router->addRoute('api/([^\\.]+)', [My\Controllers\Api::class, 'init'], HttpMethod::GET, ['request']);
    // $router->addRoute('docs/(\d{1,3}(?:\.[\\dx]{1,3}){0,2})/([a-zA-Z0-9_-]+)', [My\Controllers\Documentation::class, 'init'], HttpMethod::GET, ['version', 'docpath']);
    // $router->addRoute('docs/(\d{1,3}(?:\.[\\dx]{1,3}){0,2})', [My\Controllers\Documentation::class, 'init'], HttpMethod::GET, ['version']);
    // $router->addRoute('registration', [My\Controllers\Registration::class, 'init']);
    // $router->addRoute('registration', [My\Controllers\Registration::class, 'init'], HttpMethod::POST);
    // $router->addRoute('logout', [My\Controllers\Logout::class, 'init']);

    return $router;
}

Route Syntax

  • Path: The URI pattern to match. Regex is allowed.
  • Target: Either a controller class and method pair (e.g. [Class::class, 'method']) or a direct file path (e.g. "setup/index.php").
  • Method (optional): HTTP method (GET, POST, etc.). Default is GET.
  • Params (optional): List of parameter names for capturing values from the URI pattern.

Dispatching Requests

The following block should be included in your index.php file to initialize routing and enable automatic caching:

$r = new Router();
if (Config::get("cache_enabled") && Config::get("cache_routing_enabled")) {
    $cache_routing_key = Config::get("cache_routing_key");
    if (Cache::has($cache_routing_key)) {
        $json = Cache::get($cache_routing_key);
        $r = Router::fromArray(json_decode($json, true));
    } else {
        $r = initRoutes();
        Cache::set($cache_routing_key, json_encode($r));
    }
} else {
   $r = initRoutes();
}

$r->dispatch(Request::getServerParam('REQUEST_URI'));

Note: To enable routing cache, set the cache_routing_enabled flag to true in your config/.env.php file.

Accessing Route Parameters via Request

When a route uses regular expressions with named parameters (as configured via the fourth argument of addRoute()), the captured values are automatically injected into the Request object. You can retrieve them using the Request::getQueryParam() method in your controller:

namespace My\Controllers;

class Documentation extends \My\Controller
{
    public static function init()
    {
        $version = Request::getQueryParam('version');
        $docpath = Request::getQueryParam('docpath');
        echo "Requested documentation version: $version";
        echo "Documentation path: $docpath";
    }
}

This works for any route like:

$router->addRoute(
    'docs/(\d{1,3}(?:\.[\\dx]{1,3}){0,2})/([a-zA-Z0-9_-]+)',
    [My\Controllers\Documentation::class, 'init'],
    HttpMethod::GET,
    ['version', 'docpath']
);

Note: If a parameter is missing or unmatched, Request::getQueryParam() will return null.


Legacy Routing with .htaccess (Backward Compatibility)

This method is still supported for backward compatibility but is no longer the recommended approach.

Step 1: Add Rewrite Rule

Open the .htaccess file in the "public" folder, and add the following rule in the Rewrite Rules block:

RewriteRule ^course$ course.php [L]
Now you can create the course.php page inside the public folder. All requests to urlpath/course will be redirected to the course.php page.

Step 2: Page course.php

Insert the following code into the course.php page:

<?php
// load Boostack Environment
require __DIR__ . '/../vendor/autoload.php';
Boostack\Environment::init();

// call the custom controller Course stored in the My/Controller folder
My\Controllers\Course::init();
?>

Step 3: Controller CourseController.php

Create your custom controller CourseController.php in the My/Controllers folder:

<?php
namespace My\Controllers;

class CourseController extends \My\Controller
{
    public static function init()
    {
        parent::init();
        echo "This is the courses page";
        // insert here your custom logics
    }
}?>

If you want to manage the course route using the new Router class instead of .htaccess, you can simply register it like this in initRoutes():

$router->addRoute('course', [My\Controllers\CourseController::class, 'init']);