When you have a lot of routes, that have parameters, consider using the router’s cache file to speed up performance.

To do this, you set the routerCacheFile setting to a valid file name. The next time the app is run, then the file is created which contains an associative array with data that means that the router doesn’t need to recompile the regular expressions that it uses.

For example:

$config = [
    'settings' => [
        'addContentLengthHeader' => false,
        'displayErrorDetails' => true, // set to false in production
        'routerCacheFile' => __DIR__ . '/../routes.cache.php',
    ],
];

$app = new SlimApp($config);

Note that there’s no invalidation on this cache, so if you add or change any routes, you need to delete this file. Generally, it’s best to only set this in production.

As a very contrived example to show how it works, consider this code:

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

$config = [
    'settings' => [
        'addContentLengthHeader' => false,
        'displayErrorDetails' => true, // set to false in production
        'routerCacheFile' => __DIR__ . '/../routes.cache.php',
    ],
];

$app = new SlimApp($config);

for ($groupName = 1; $groupName group("/$groupName", function() {
        for ($i = 1; $i get("/$i/{id:d+}", AppAction::class);
            $this->post("/$i/{id:d+}", AppAction::class);
            $this->put("/$i/{id:d+}", AppAction::class);
            $this->delete("/$i/{id:d+}", AppAction::class);
        }
    });
}

// Run!
$start = microtime(true);
$app->run();
$diff = microtime(true) - $start;
echo "n" . sprintf("%0.03f", $diff * 1000);

This application creates 25 groups, each with 4000 routes, each of which has a placeholder parameter with a constraint. That’s quite a lot of routes, but things take long enough that we can see timing. The AppAction does nothing.

On my computer, using PHP 7.0.18’s built-in web server, the first time we run it, we see this:

$ curl "http://localhost:8888/1/2/3"
2722.414

This took 2.7 seconds to execute. At the same time, it also created a file called routes.cache.php which is then used for the next run:

$ curl "http://localhost:8888/1/2/3"
263.228

This time, it took just 263ms.

That’s a big difference!

If you have a lot of complex routes in your Slim application, then I recommend that you test whether enabling route caching makes a difference.

Source: AKRABAT

By Rob