Installation:
composer require alexandermatveev/fancybox-bundle
Ensure your project uses Symfony 3+ (compatible with Symfony 2.1+ in dev).
Enable the Bundle:
Add to config/bundles.php (Symfony 4+) or AppKernel.php (Symfony 3):
Alexandermatveev\FancyboxBundle\AlexandermatveevFancyboxBundle::class => ['all' => true],
First Use Case:
Include assets in a Twig template (e.g., base.html.twig):
<link href="{{ asset('bundles/alexandermatveevfancybox/dist/jquery.fancybox.min.css') }}" rel="stylesheet">
<script src="{{ asset('bundles/alexandermatveevfancybox/dist/jquery.fancybox.min.js') }}"></script>
Initialize Fancybox for a gallery:
<a data-fancybox="gallery" href="image.jpg">Image 1</a>
<a data-fancybox="gallery" href="image2.jpg">Image 2</a>
Verify:
Check the Fancybox 3 docs for basic usage (e.g., data-fancybox, data-src, data-caption).
Basic Image Gallery:
Use data-fancybox="gallery" for grouped images. Example:
<div class="gallery">
{% for image in images %}
<a data-fancybox="gallery" href="{{ asset(image.path) }}" data-caption="{{ image.title }}">
<img src="{{ asset(image.thumbnail) }}" alt="{{ image.title }}">
</a>
{% endfor %}
</div>
Dynamic Initialization: Initialize Fancybox via JavaScript after DOM load (e.g., for AJAX-loaded content):
$(document).on('click', '[data-fancybox]', function() {
$.fancybox.open($(this));
});
Custom Templates:
Override Fancybox’s default UI by extending its CSS/JS. Place custom files in web/bundles/alexandermatveevfancybox/ to override bundle assets.
Symfony Integration:
asset() for paths (e.g., {{ asset('bundles/...') }}).// src/Twig/FancyboxExtension.php
class FancyboxExtension extends \Twig\Extension\AbstractExtension
{
public function getFunctions()
{
return [
new \Twig\TwigFunction('fancybox_link', [$this, 'generateFancyboxLink']),
];
}
public function generateFancyboxLink(string $href, string $caption = null): string
{
$attributes = ['data-fancybox="gallery"', 'href="' . htmlspecialchars($href) . '"'];
if ($caption) {
$attributes[] = 'data-caption="' . htmlspecialchars($caption) . '"';
}
return '<a ' . implode(' ', $attributes) . '></a>';
}
}
Usage in Twig:
{{ fancybox_link(asset(image.path), image.title) }}
Lazy Loading:
Combine with lazy-loading libraries (e.g., ozymko/vite-plugin-lazy) for performance:
<a data-fancybox data-src="{{ asset(image.path) | lazyload }}" href="javascript:;">
<img src="placeholder.jpg" data-src="{{ asset(image.thumbnail) }}" loading="lazy">
</a>
Asset Paths:
bundles/alexandermatveevfancybox/. If using Symfony Flex or custom paths, override via config/packages/alexandermatveev_fancybox.yaml (if supported) or manually update paths in Twig.jQuery Dependency:
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="{{ asset('bundles/alexandermatveevfancybox/dist/jquery.fancybox.min.js') }}"></script>
Outdated Version:
vendor/alexandermatveev/fancybox-bundle/ with latest Fancybox 3.Twig Autoloading:
Resources/views/ (though this bundle lacks Twig templates).License Compliance:
Console Errors:
Uncaught TypeError if jQuery is missing or loaded after Fancybox. Use browser dev tools (F12) to verify.Fancybox Not Initializing:
data-fancybox attributes are present. Test with a hardcoded link first:
<a data-fancybox href="https://example.com/image.jpg">Test</a>
Custom CSS Overrides:
!important sparingly. Inspect elements to confirm your CSS is loaded after Fancybox’s.Custom Events:
afterLoad) via jQuery:
$(document).on('click', '[data-fancybox]', function() {
$.fancybox.open({
src: this.href,
afterLoad: function() {
console.log('Image loaded');
}
});
});
API Integration:
$.fancybox.close(); // Close current instance
$.fancybox.open({ src: '/new-image.jpg' }); // Open a new image
Symfony Event Listeners:
kernel.response):
// src/EventListener/FancyboxListener.php
class FancyboxListener implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
return [KernelEvents::RESPONSE => 'onKernelResponse'];
}
public function onKernelResponse(FilterResponseEvent $event)
{
$response = $event->getResponse();
if ($response->headers->contains('Content-Type', 'text/html')) {
$this->addFancyboxJS($response);
}
}
private function addFancyboxJS(Response $response)
{
$content = $response->getContent();
$content = str_replace('</body>', '
<script src="' . asset('bundles/alexandermatveevfancybox/dist/jquery.fancybox.min.js') . '"></script>
<script>$(document).ready(function(){$("[data-fancybox]").fancybox();});</script>
</body>', $content);
$response->setContent($content);
}
}
Webpack Encore:
webpack.config.js:
Encore
.addEntry('fancybox', './vendor/alexandermatveev/fancybox-bundle/Resources/public/js/fancybox.js')
.enableSingleRuntimeChunk()
.copyFiles({
from: './vendor/alexandermatveev/fancybox-bundle/Resources/public/css/',
to: 'bundles/alexandermatveevfancybox/[path][name].[hash].[ext]',
});
How can I help you explore Laravel packages today?