Development

Full route caching for Slim Framework

Today I am releasing Slim Turbo. This provides a full route caching mechanism for Symfony’s Dependency Injection container, and for PHP-DI.

The story started a while ago when I tried using Slim and Symfony’s Dependency Injector together for a project. It failed, but it wasn’t because the two couldn’t work together, it was because after the DI container was compiled, I couldn’t use urlFor. I was at first confused as to why it wouldn’t work, but then found out that Slim doesn’t cache the named routes. Slim does have a setting to cache the FastRoute generated data, but this doesn’t help us when using named routes, and compiled containers.

This is where Slim Turbo steps up to the plate, and gives you the ability to have full route caching: named routes, and generated route data.

The mechanism for defining routes changes when using Slim Turbo. You will need to create a class that implements the Slim\Turbo\Provider\RouteProvider interface, then register it with your container. When using the appropriate provider for your container, the route generation will apply the routes similarly to add them to Slim\App. By setting a SimpleCache implementation at routing.cache in your container both the named routes and generated data will be saved for future use.

If you use a compiled DI container other than Symfony’s or PHP-DI, please send a PR with the implementation!

Leave a reply