symfony/ux-icons
Symfony UX Icons provides a simple way to use SVG icon packs in Symfony apps. Install popular sets, render icons in Twig or PHP, and manage them consistently across your UI with the Symfony UX tooling and asset pipeline.
composer require symfony/ux-icons
composer require symfony/twig-bundle
config/app.php:
'providers' => [
// ...
Symfony\Bundle\TwigBundle\TwigBundle::class,
],
config/twig.php):
return [
'paths' => [
// ...
__DIR__.'/../resources/views/icons' => 'icons',
],
];
{{ ux_icon('mdi:check', {class: 'w-4 h-4'}) }}
resources/views/icons/mdi/check.svg.twig (or use remote icons via Iconify).{# resources/views/admin/dashboard.twig #}
{% for user in users %}
<div class="flex items-center">
{{ ux_icon(
user.role === 'admin' ? 'mdi:shield' : 'mdi:account',
{class: 'w-5 h-5 mr-2'}
) }}
<span>{{ user.name }}</span>
</div>
{% endfor %}
resources/
└── views/
└── icons/
├── mdi/
│ ├── check.svg.twig
│ └── home.svg.twig
└── custom/
└── logo.svg.twig
resources/views/icons/mdi/check.svg.twig):
<svg viewBox="0 0 24 24" fill="currentColor" {% for attr, value in attributes %}{{ attr }}="{{ value }}"{% endfor %}>
<path d="M9 16.17L4.59 12 8.19 8.41 10 10.29 14.59 5.71 16.41 7.59 10 14z"/>
</svg>
{{ ux_icon('mdi:check', {class: 'text-green-500'}) }}
config/packages/ux_icons.yaml):
ux_icons:
icon_sets:
mdi: https://cdn.jsdelivr.net/npm/@mdi/js@7.2.97/svg/iconify.json
ignore_not_found: true
{{ ux_icon('mdi:home', {class: 'w-6 h-6'}) }}
resources/views/components/icon.twig):
<twig:ux:icon
name="{{ name }}"
class="{{ class ?? 'w-4 h-4' }}"
aria-label="{{ aria_label ?? '' }}"
/>
@twig('components/icon.twig', ['name' => 'mdi:check', 'class' => 'text-red-500'])
app/Helpers/IconHelper.php):
namespace App\Helpers;
use Symfony\UX\Icons\IconRenderer;
class IconHelper {
public static function render(string $name, array $options = []): string {
$renderer = new IconRenderer();
return $renderer->render($name, $options);
}
}
{!! \App\Helpers\IconHelper::render('mdi:home', ['class' => 'w-5 h-5']) !!}
config/ux_icons.yaml):
ux_icons:
icon_sets:
custom: /path/to/local/icons
aliases:
home: mdi:home
user: mdi:account
{{ ux_icon('home', {class: 'w-5 h-5'}) }} {# Renders mdi:home #}
// app/Providers/IconServiceProvider.php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Symfony\UX\Icons\IconRenderer;
use Illuminate\Support\Facades\Cache;
class IconServiceProvider extends ServiceProvider {
public function boot() {
$renderer = new IconRenderer();
$renderer->setIconLoader(function ($name) {
return Cache::remember(
"icon_{$name}",
now()->addHours(1),
function () use ($name) {
// Custom logic to fetch remote/local SVGs
return file_get_contents("https://cdn.example.com/icons/{$name}.svg");
}
);
});
}
}
{{ ux_icon('mdi:check', {class: 'w-6 h-6 text-blue-500'}) }}
{{ ux_icon('mdi:heart', {
class: 'w-5 h-5',
'fill': 'currentColor',
'stroke-width': '1.5'
}) }}
// app/Http/Livewire/Admin/UserIcon.php
namespace App\Http\Livewire;
use Livewire\Component;
use App\Helpers\IconHelper;
class UserIcon extends Component {
public $userRole;
public function render() {
return view('livewire.admin.user-icon', [
'icon' => IconHelper::render(
$this->userRole === 'admin' ? 'mdi:shield' : 'mdi:account',
['class' => 'w-5 h-5']
),
]);
}
}
<div>
{!! $icon !!}
<span>{{ $user->name }}</span>
</div>
Twig vs. Blade Conflicts:
ux_icon) won’t work in Blade templates by default.@twig directives or create a Blade wrapper:
@php
$iconRenderer = new \Symfony\UX\Icons\IconRenderer();
echo $iconRenderer->render('mdi:check', ['class' => 'w-4 h-4']);
@endphp
Remote SVG Caching Headaches:
Cache::forever() for static icons.XMLNS Attribute Breaking Tests:
xmlns to SVGs, breaking string-based assertions in tests.$renderer = new IconRenderer();
$renderer->lockOnDemand(true); // Force-lock all icons
Icon Not Found Errors:
ux_icon() throws exceptions for missing icons.ignore_not_found in config:
ux_icons:
ignore_not_found: true
SVG Security Risks:
$renderer->setIconLoader(function ($name) {
$url = "https://trusted-cdn.com/icons/{$name}.svg";
$svg = @file_get_contents($url);
if (!$svg || strpos($svg, '<!DOCTYPE') !== false) {
throw new \RuntimeException("Invalid SVG: {$name}");
}
return $svg;
});
Twig Component Caching:
<twig:ux:icon> may not respect Laravel’s cache tags.
How can I help you explore Laravel packages today?