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.
Register the manager
Section titled “Register the manager”Without a DI container
Section titled “Without a DI container”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);With a DI container
Section titled “With a DI container”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; },];Use with PHP-View
Section titled “Use with PHP-View”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>Use with Twig-View
Section titled “Use with Twig-View”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>Fluent chaining
Section titled “Fluent chaining”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) ?>Check for existence
Section titled “Check for existence”Use the static facade to conditionally render icons:
<?php if (SwarmIcons::has('heroicons:user')): ?> <?= swarm_icon('heroicons:user') ?><?php endif; ?>Where to put your icons
Section titled “Where to put your icons”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/See also
Section titled “See also”- Twig Integration: full Twig function reference (
icon(),icon_exists(),get_icon()) - Fluent API: all available methods on the
Iconvalue object - Accessibility: decorative vs meaningful icons and ARIA best practices