alexandermatveev/bootstrap4-bundle
Install Dependencies Run:
composer require alexandermatveev/bootstrap4-bundle alexandermatveev/jquery-bundle alexandermatveev/popper-bundle
Ensure jquery-bundle and popper-bundle are installed as prerequisites.
Register the Bundle
Add to app/AppKernel.php:
new AlexanderMatveev\Bootstrap4Bundle\AlexanderMatveevBootstrap4Bundle(),
Install Assets
Symlink assets to your web directory:
bin/console assets:install --symlink web
First Use Case
Include Bootstrap in a Twig template (e.g., base.html.twig):
<link href="{{ asset('bundles/alexandermatveevbootstrap4/lib/css/bootstrap.min.css') }}" rel="stylesheet">
<script src="{{ asset('bundles/alexandermatveevbootstrap4/lib/js/bootstrap.min.js') }}"></script>
Test with a basic component (e.g., a modal):
<button class="btn btn-primary" data-toggle="modal" data-target="#exampleModal">Launch Modal</button>
<div class="modal fade" id="exampleModal">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Modal Title</h5>
</div>
<div class="modal-body">Modal content here.</div>
</div>
</div>
</div>
Asset Management
asset() helper for paths (e.g., asset('bundles/alexandermatveevbootstrap4/lib/js/bootstrap.bundle.min.js')).config/packages/assetic.yaml:
version: '1.0.*'
Component Integration
form_theme (e.g., form_div_layout.html.twig) to style forms with Bootstrap classes.
{% form_theme form 'bootstrap4_layout.html.twig' %}
$(document).ready(function() {
$('[data-toggle="tooltip"]').tooltip();
});
Customization
vendor/alexandermatveev/bootstrap4-bundle/ to web/bundles/alexandermatveevbootstrap4/ and modify them.Lazy Loading
// In your main JS file
if (document.querySelector('[data-toggle="modal"]')) {
import(/* webpackChunkName: "bootstrap" */ 'bootstrap.min.js');
}
symfony/ux-turbo) for modern interactivity while retaining Bootstrap’s styling.// webpack.config.js
Encore
.addEntry('bootstrap', './vendor/alexandermatveev/bootstrap4-bundle/lib/js/bootstrap.min.js')
.enableSingleRuntimeChunk()
.copyFiles({
from: './vendor/alexandermatveev/bootstrap4-bundle/lib/css/',
to: 'bundles/alexandermatveevbootstrap4/lib/css/[name].[ext]',
});
// src/Twig/BootstrapExtension.php
class BootstrapExtension extends \Twig\Extension\AbstractExtension
{
public function getFunctions()
{
return [
new \Twig\TwigFunction('bootstrap_button', [$this, 'renderButton']),
];
}
public function renderButton(array $options)
{
return sprintf(
'<button class="btn btn-%s">%s</button>',
$options['type'] ?? 'primary',
$options['label']
);
}
}
Usage in Twig:
{{ bootstrap_button({'label': 'Click', 'type': 'success'}) }}
Asset Paths
/bundles/...) breaks when the bundle is updated. Always use asset().{% set bootstrap_css = asset('bundles/alexandermatveevbootstrap4/lib/css/bootstrap.min.css') %}).Dependency Conflicts
jquery-bundle and popper-bundle. Mixing with globally loaded jQuery/Popper may cause issues.<script src="{{ asset('bundles/alexandermatveevjquery/lib/jquery.min.js') }}"></script>
<script src="{{ asset('bundles/alexandermatveevpopper/lib/popper.min.js') }}"></script>
Outdated Bundle
Asset Installation
--symlink may fail on some servers. Use --no-debug mode for testing:
APP_ENV=prod bin/console assets:install --symlink web
Console Errors
Uncaught ReferenceError or bootstrap is not defined. Ensure:
$ or jQuery.Component Not Working
data-toggle="modal"). Bootstrap 4 uses data-bs-* in newer versions, but this bundle uses the older syntax.CSS Overrides
!important sparingly..my-component .btn { ... }).Versioning
composer.json to avoid unexpected updates:
"require": {
"alexandermatveev/bootstrap4-bundle": "dev-main"
}
Performance
Testing
// tests/Feature/BootstrapModalTest.php
public function testModalWorks()
{
$this->visitPath('/test-modal');
$this->click('Launch Modal');
$this->assertSelectorTextContains('.modal-title', 'Modal Title');
}
Extension Points
bootstrap_components.html.twig file in your templates directory to override default components.bootstrap.min.js:
<script src="{{ asset('bundles/alexandermatveevbootstrap4/lib/js/bootstrap.min.js') }}"></script>
<script src="{{ asset('js/custom-bootstrap-plugins.js') }}"></script>
Configuration
config/packages/bootstrap4.yaml:
bootstrap4:
enabled: true
debug: '%kernel.debug%'
assets:
css: 'bundles/alexandermatveevbootstrap4/lib/css/bootstrap.min.css'
js: 'bundles/alexandermatveevbootstrap4/lib/js/bootstrap.min.js'
{% set bootstrap_css = app.config.bootstrap4.assets.css %}
How can I help you explore Laravel packages today?