Conditional Route Registering based on Current request

Conditional Route Registering based on http Current request.

Below listed is a rough sample of top level Routes ordering for slim based medium sized PHP project. There are 500 + and growing end Routes to be registered.

The focus is to place just one file ( index.php ) under public document. and a main file named main.php above document root. Instead of registering all routes for every request, Routes are registered based on the current http request path. There by registering only necessary endpoints ( per request ), instead of registering all 500+ end points.

Below listed is a top level code. The actual route registering happens inside the static function of corresponding Controller Class.

Please provide your valuable Comments / Guidance / Suggestions / Fix.
Looking to improve the structure or Better alternatives.

Thank You
Susanth K

==================================================

FileName : /home/public/index.php

<?php
require_once __DIR__ . "/../app/src/main.php";

==================================================

FileName : /home/app/src/main.php

<?php
// Session Code
// ...
require_once __DIR__ . "/../../composer/vendor/autoload.php";
require_once __DIR__ . "/app-autoload.php";

$slimSettings['settings']['displayErrorDetails']    = false;
$slimSettings['settings']['addContentLengthHeader'] = false;

$app = new \Slim\App($slimSettings);

// Security Check Code
// if any ...

addRoutesForCurrentPath($app);

addCommonRoutes($app);

$app->run();

function addRoutesForCurrentPath($app)
{
    $path = $app->getContainer()['request']->getUri()->getPath();

    if (stripos($path, '/site/') === 0)
        \App\WebSiteController::addRoutes($app);
    else if (stripos($path, '/blog/') === 0)
        \App\BlogController::addRoutes($app);
    else if (stripos($path, '/cms/') === 0)
        \App\CMSController::addRoutes($app);
    else if (stripos($path, '/api/public/') === 0)
        \App\PublicAPIController::addRoutes($app);
    else if (stripos($path, '/api/private/') === 0)
        \App\PrivateAPIController::addRoutes($app);
    else if (stripos($path, '/user-db/') === 0)
        \App\UserDashBoardController::addRoutes($app);
    else if (stripos($path, '/staff-db/') === 0)
        \App\StaffDashBoardController::addRoutes($app);
    else if (stripos($path, '/admin-db/') === 0)
        \App\AdminDashBoardController::addRoutes($app);
}

function addCommonRoutes($app)
{
    \App\ErrorHandler::addRoutes($app);
    // ... Add other common routes, here
}

==================================================

NOTE : Project Directory Structure

/home/app/src/    # PHP App Source Code
/home/composer/   # PHP Composer Vendor source Code
/home/public/     # Public Document root of Website
/home/public/index.php  # The one and only php file under public web root
/home/public/.htaccess  # Web server config file
/home/public/asset/     # image / js / css bundles

==================================================

NOTE : List of Routes Grouped

@ CMS : Generic Web Page end points
    /site/page-one ...
    /site/page-n
    /site/category-one/page-one ...
    /site/category-n/sub-category-n/page-n
    // -----( 50 + web page routes )-----

@ CMS : Blog Page end points
    /blog/topic-one ...
    /blog/topic-n
    /blog/group-one/topic-one ...
    /blog/group-n/sub-group-n/topic-n
    // -----( 100 + blog page routes )-----

@ CMS : User/Admin Dashboard Page end points
    /cms/user/page/one ...
    /cms/user/page/n
    /cms/admin/page/one ...
    /cms/admin/page/n
    // -----( 50 + cms dashboard page routes )-----

@ Public Web API end points
    /api/public/node/one ...
    /api/public/node/n
    // -----( 50 + public api routes )-----

@ Private Web API end points
    /api/public/node/one ...
    /api/public/node/n
    // -----( 150 + private api routes )-----

@ Registered Users Dashboard Web app end points
    /user-db/user-id/page/one ...
    /user-db/user-id/page/n
    // -----( 50 + private api routes )-----

@ Staff Dashboard Web app end points
    /staff-db/staff-id/page/one ...
    /staff-db/staff-id/page/n
    // -----( 50 + private api routes )-----

@ Admin Dashboard Web app end points
    /admin-db/admin-id/page/one ...
    /admin-db/admin-id/page/n
    // -----( 50 + private api routes )-----

Roughly 500+ and growing list of route end points.

==================================================

In Slim 3 there is little need for this… Slim comes with FastRoute and you may want to use the cached router to speed up dispatching.

http://www.slimframework.com/docs/objects/application.html#slim-default-settings

set routerCacheFile to a writable file.

Keep in mind that you then need to destroy this file as part of your update processes should any routes change.

It’s hard to say if your solution has any value. I personally would never bother with this sort of approach until there is a problem with requests being fulfilled in a timely manner.

This solution seems like the classic premature optimization.

1 Like

@geggleto Thank you for your suggestions. Let me look into cache Settings.