I’ve updated Slim Framework 3.0 to 3.5 on my server and I noticed that a new option named
routerCacheFile for settings have been added.
So as the documentation says I’ve choosen a valid PHP writable file so I can test it and see how it works. Everything worked fine, the file was filled with the route caching on the first run.
My question is: What benefits does this brings ? The answer can be in the PRO/CON format. I want just to know if this impact somehow in the perfomance since is disabled by default.
Note that I don’t know what this option does, would be appreciated if someone explained to me in a few words this options.
It is used for the caching in FastRoute
public function dispatch(ServerRequestInterface $request)
$uri = '/' . ltrim($request->getUri()->getPath(), '/');
* Create a new Route object
* @param string $methods Array of HTTP methods
* @param string $pattern The route pattern
* @param callable $callable The route callable
* @return RouteInterface
The benefit of caching should be performance.
I guess the reason its off by default it that it only makes sense in production.
Caching will improve performance especially when there are more number of endpoints. say for example 500+
I was not aware of this option and came with a solution for my new project :
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 end…
routerCacheFile option should makes things simple.
Please have a look at the Comments received
To QUICKLY view performance difference, with and without caching : integrate
Tracy - PHP debugger.
The entire site is
interactive, try it. Tracy bar shows execution time.
Explore by fetching random routes with and without cache.
Delete the cache file to start fresh.
#Tracy debugger stopwatch
( copy paste from github project page )
Another useful tool is the debugger stopwatch with a precision of microseconds:
// sweet dreams my cherrie
$elapsed = Debugger::timer();
// $elapsed = 2
Multiple measurements at once can be achieved by an optional parameter.
// some code
// some code
$rssElapsed = Debugger::timer('rss-generating');
$pageElapsed = Debugger::timer('page-generating');
Debugger::timer(); // runs the timer
... // some time consuming operation
echo Debugger::timer(); // elapsed time in seconds
More about debugger for slim3
It would seem that this is used only in FastCache. From the point of view of a slim application, does this mean that my ‘workflow’ still requires me to define all my routes but than I simply do a
after defining my routes? Also, where in the application should this be placed? I think router is created in Slim itself, so what’s the proper place to do a setCacheFile() ?