Installation:
composer require bmatzner/leaflet-bundle:~0.7
php app/console assets:install web --symlink
Register the bundle in app/AppKernel.php under registerBundles().
First Use Case:
Include the bundle assets in a Twig template (e.g., base.html.twig):
<link rel="stylesheet" href="{{ asset('bundles/bmatznerleaflet/css/leaflet.css') }}">
<script src="{{ asset('bundles/bmatznerleaflet/js/leaflet.min.js') }}"></script>
Initialize a basic map in a Twig template:
<div id="map" style="height: 400px;"></div>
<script>
var map = L.map('map').setView([51.505, -0.09], 13);
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png').addTo(map);
</script>
vendor/bmatzner/leaflet-bundle/Resources/public/ for included assets.{{ asset() }} for dynamic paths in Twig.Dynamic Map Initialization: Pass map options/config via Twig variables or Twig extensions:
{% set mapConfig = {
center: [lat, lng],
zoom: 13,
layers: ['osm', 'satellite']
} %}
Render in JavaScript:
var map = L.map('map').setView([{{ mapConfig.center[0] }}, {{ mapConfig.center[1] }}], {{ mapConfig.zoom }});
Reusable Map Components: Create a Twig macro for consistent map setups:
{% macro leafletMap(id, center, zoom) %}
<div id="{{ id }}" style="height: 400px;"></div>
<script>
var {{ id }} = L.map('{{ id }}').setView({{ center }}, {{ zoom }});
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png').addTo({{ id }});
</script>
{% endmacro %}
Integration with Symfony Forms: Use Leaflet for geolocation forms (e.g., address fields). Store coordinates in a hidden form field:
<input type="hidden" id="map-coordinates" name="address[coordinates]">
<script>
map.on('click', function(e) {
document.getElementById('map-coordinates').value = e.latlng.lat + ',' + e.latlng.lng;
});
</script>
Asset Versioning: Append a query string to cache-bust Leaflet assets:
<script src="{{ asset('bundles/bmatznerleaflet/js/leaflet.min.js?v=' ~ '1.0') }}"></script>
assets:install with Encore for modern asset pipelines.leaflet-markercluster) by including their JS/CSS via asset().Outdated Leaflet Version:
web/bundles/bmatznerleaflet/ and update references in Twig.Asset Path Assumptions:
web/bundles/bmatznerleaflet/. Custom paths require manual updates to asset() calls.--symlink in assets:install to avoid duplication.IE8 Support:
leaflet.ie.css for IE8, but modern projects may not need this. Remove if unused to reduce load time.No Twig Integration:
json_encode to pass PHP data to Leaflet:
{{ dump(mapMarkers|json_encode|raw) }}
Missing Documentation:
assets:install ran and paths in Twig match the installed assets.console.log() in Leaflet callbacks to debug events.Custom Templates:
Override bundle templates by creating BmatznerLeafletBundle::leaflet.html.twig in your theme.
Asset Overrides:
Replace default assets by copying files to web/bundles/bmatznerleaflet/ and updating asset() paths.
Symfony Events:
Listen to kernel.request to dynamically modify map configurations:
$event->getRequest()->attributes->set('map_config', ['center' => [48.85, 2.35]]);
Access in Twig:
{{ dump(app.request.attributes.get('map_config')) }}
Leaflet Plugins: Include third-party plugins by extending the asset pipeline:
<script src="{{ asset('bundles/bmatznerleaflet/js/plugins/leaflet.markercluster.js') }}"></script>
data-turbo-track="reload" in Symfony UX Turbo).asset() with ?v=1.0 for cache busting and enable HTTP/2 for asset multiplexing.leaflet-image).How can I help you explore Laravel packages today?