vendredi 18 décembre 2015

Front controller vs static router memory usage

Excuse my ignorance, but how does using a front controller compare in memory usage to using a static router? I'm particularly curious to how this applies in PHP.

Example of front controller (pseudo-code)

// index.php

$path = getRequestedPath();
$class = 'Controller_'.$path;

if (classExists($class)) {
    $ctr = new $class($req, $resp);
    $ctr->execute();
} else {
    $resp->setError(404);
}

// ...

Example of static router (I may be calling this the wrong thing)

// index.php

$router = new Router();

$router->get('/', function($req, $resp){
    // instantiate service classes
    // use service classes
});

$router->get('/products', function($req, $resp){
    // instantiate service classes
    // use service classes
});

$router->get('/product/:productId', function($req, $resp){
    // instantiate service classes
    // use service classes
});

$router->post('/product/:productId', function($req, $resp){
    // instantiate service classes
    // use service classes
});

// ...

Seems like the frameworks of yesterday were all based on the first pattern. Now I'm under the impression that the new trend is to use something similar to the second example, where you list every possible path to your app/api.

Is it just me, or does the second example really allocates memory to lay the entire app in memory, although only a single path is served per request?

Aucun commentaire:

Enregistrer un commentaire