Skip to content

Slim Framework

Swarm Icons works with Slim out of the box. Register the manager in your bootstrap or DI container, then use icons in PHP templates or Twig views.

If you’re not using a DI container, drop the setup straight into your bootstrap file (public/index.php, app/bootstrap.php, etc.) before your routes:

use Frostybee\SwarmIcons\SwarmIcons;
use Frostybee\SwarmIcons\SwarmIconsConfig;
$manager = SwarmIconsConfig::create()
->discoverJsonSets('path/to/resources/json')
->cachePath('path/to/var/cache/icons')
->defaultPrefix('tabler')
->defaultAttributes(['class' => 'icon'])
->build();
SwarmIcons::setManager($manager);

If your Slim app uses a DI container like PHP-DI, wire up the manager in your container definitions:

use Frostybee\SwarmIcons\SwarmIcons;
use Frostybee\SwarmIcons\SwarmIconsConfig;
use Frostybee\SwarmIcons\IconManager;
return [
IconManager::class => function () {
$manager = SwarmIconsConfig::create()
->discoverJsonSets('path/to/resources/json')
->cachePath('path/to/var/cache/icons')
->defaultPrefix('tabler')
->defaultAttributes(['class' => 'icon'])
->build();
SwarmIcons::setManager($manager);
return $manager;
},
];

If you use slim/php-view, call the swarm_icon() helper directly in your .php templates:

<nav>
<?= swarm_icon('home') ?>
<?= swarm_icon('heroicons:user', ['class' => 'w-6 h-6']) ?>
<?= swarm_icon('tabler:settings', ['aria-label' => 'Settings']) ?>
</nav>

If you use slim/twig-view, register the Swarm Icons extension on the Twig environment:

use Slim\Views\Twig;
use Frostybee\SwarmIcons\Twig\SwarmIconsExtension;
$twig = Twig::create('path/to/templates');
$twig->addExtension(new SwarmIconsExtension());

Then use icon() in your .twig templates:

<nav>
{{ icon('home') }}
{{ icon('heroicons:user', {class: 'w-6 h-6'}) }}
{{ icon('tabler:settings', {'aria-label': 'Settings'}) }}
</nav>

Both the PHP helper and the Twig get_icon() function return an Icon object that supports fluent method chaining:

<?= swarm_icon('home')->size(24)->class('text-blue-500') ?>
<?= swarm_icon('tabler:star')->fill('currentColor')->strokeWidth(1.5) ?>

Use the static facade to conditionally render icons:

<?php if (SwarmIcons::has('heroicons:user')): ?>
<?= swarm_icon('heroicons:user') ?>
<?php endif; ?>

A typical Slim project might organize icon files like this:

project/
├── resources/
│ ├── json/ ← downloaded JSON collections
│ │ ├── tabler.json
│ │ ├── heroicons.json
│ │ └── mdi.json
│ └── icons/ ← custom SVG icons (if any)
│ ├── logo.svg
│ └── brand.svg
├── templates/ ← Twig or PHP-View templates
├── var/cache/icons/ ← icon cache directory
└── public/
  • Twig Integration: full Twig function reference (icon(), icon_exists(), get_icon())
  • Fluent API: all available methods on the Icon value object
  • Accessibility: decorative vs meaningful icons and ARIA best practices