moox/file-icons
Laravel/PHP package providing a set of file type icons (SVG), sourced from the SVGRepo “file-types” collection. Useful for displaying consistent icons for common document and media extensions in your app’s UI.
Installation:
composer require moox/file-icons
Publish the config file (if needed):
php artisan vendor:publish --provider="Moox\FileIcons\FileIconsServiceProvider"
Basic Usage:
Register the service provider in config/app.php (if not auto-discovered):
'providers' => [
Moox\FileIcons\FileIconsServiceProvider::class,
],
First Use Case: Display an icon for a file in a Blade view:
{!! file_icon('document.pdf') !!}
Or with custom size:
{!! file_icon('document.pdf', '2x') !!}
Check config/file-icons.php for:
Dynamic File Icon Rendering: Use in file managers, CMS previews, or code editors:
@foreach($files as $file)
<div class="file-item">
{!! file_icon($file->extension) !!}
<span>{{ $file->name }}</span>
</div>
@endforeach
Custom Icon Sets: Override defaults via config:
'icons' => [
'pdf' => 'custom-pdf-icon.svg',
'default' => 'custom-fallback.svg',
],
Integration with Laravel Filesystem: Fetch icons dynamically from stored files:
$icon = file_icon($file->extension, '1x');
Conditional Icons: Combine with Laravel helpers:
@if($file->isImage())
{!! file_icon('image', '2x') !!}
@else
{!! file_icon($file->extension) !!}
@endif
Caching Icons: Cache rendered icons in a Blade directive:
Blade::directive('cachedIcon', function ($expression) {
return "<?php echo cache()->remember('file_icon_{$expression}', now()->addHours(1), function() use({$expression}) { echo file_icon({$expression}); }); ?>";
});
Usage:
@cachedIcon('document.pdf')
Icon Size Management: Use a helper to standardize sizes:
function getIconSize($fileType) {
return in_array($fileType, ['image', 'video']) ? '2x' : '1x';
}
{!! file_icon($file->extension, getIconSize($file->extension)) !!}
Extension Mismatch:
.PDF → .pdf).strtolower() if parsing from user input:
$extension = strtolower(pathinfo($file->name, PATHINFO_EXTENSION));
Missing Icons:
config/file-icons.php for default icon settings.dd(file_icon('unknown_extension', null, true)); // Returns raw icon data
SVG Injection Risks:
{!! htmlspecialchars(file_icon($file->extension), ENT_QUOTES) !!}
Blade::escape() for safer rendering.Caching Issues:
file-icons.php:
php artisan config:clear
Log Icon Data:
\Log::debug('Icon data:', [
'extension' => $file->extension,
'raw_icon' => file_icon($file->extension, null, true),
]);
Inspect SVGRepo Collection: Check the SVGRepo collection for missing icons and update the config.
Custom Icon Resolver:
Bind a custom resolver in AppServiceProvider:
FileIcons::extend('custom', function ($extension) {
return file_get_contents(storage_path("app/icons/{$extension}.svg"));
});
Usage:
{!! file_icon('document.pdf', '1x', 'custom') !!}
Blade Directives: Create a shorthand directive:
Blade::directive('fileicon', function ($expression) {
return "<?php echo file_icon({$expression}); ?>";
});
Usage:
@fileicon('document.pdf')
Livewire/Alpine Integration: Dynamically update icons on file change:
// Alpine.js
<div x-data="{ icon: 'document.pdf' }">
@{{ $icon }}
{!! file_icon($icon) !!}
</div>
How can I help you explore Laravel packages today?