dontdrinkandroot/bootstrap-bundle
Installation Add the package via Composer (adapting for Symfony compatibility):
composer require dontdrinkandroot/bootstrap-bundle
Since Laravel doesn’t natively support Symfony bundles, create a Service Provider to bridge the gap:
php artisan make:provider BootstrapServiceProvider
Register the Bundle
In BootstrapServiceProvider.php, register the Symfony bundle:
use DontDrinkAndRoot\BootstrapBundle\BootstrapBundle;
public function register()
{
$this->app->register(new BootstrapBundle());
}
Publish Assets
Use Laravel Mix or Vite to compile Bootstrap’s CSS/JS. The bundle provides Twig integration (not native to Laravel), so manually include Bootstrap’s assets in your resources/views/layouts/app.blade.php:
<link href="{{ asset('css/bootstrap.min.css') }}" rel="stylesheet">
<script src="{{ asset('js/bootstrap.bundle.min.js') }}"></script>
First Use Case: Basic Components Use Blade directives (not Twig) to render Bootstrap components. Example:
@php
// Simulate Twig's `bootstrap_button` (custom directive)
$buttonClass = 'btn btn-primary';
$buttonText = 'Click Me';
@endphp
<button class="{{ $buttonClass }}">{{ $buttonText }}</button>
Note: For advanced Twig features, consider Laravel-Twig-Bridge.
// vite.config.js
import { defineConfig } from 'vite';
import laravel from 'laravel-vite-plugin';
export default defineConfig({
plugins: [
laravel({
input: ['resources/scss/app.scss'],
refresh: true,
}),
],
});
Include Bootstrap via @import in resources/scss/app.scss:
@import "~bootstrap/scss/bootstrap";
Create Blade components to wrap Bootstrap markup:
php artisan make:component Alert
<!-- resources/views/components/alert.blade.php -->
<div class="alert alert-{{ $type ?? 'info' }} alert-dismissible fade show" role="alert">
{{ $slot }}
<button type="button" class="btn-close" data-bs-dismiss="alert"></button>
</div>
Usage:
<x-alert type="success">Operation completed!</x-alert>
Use Laravel’s mix-manifest.json to reference Bootstrap JS:
<script src="{{ mix('js/app.js') }}"></script>
Initialize Bootstrap components (e.g., modals, tooltips) in resources/js/app.js:
import * as bootstrap from 'bootstrap';
document.addEventListener('DOMContentLoaded', () => {
new bootstrap.Modal(document.getElementById('myModal'));
});
Combine Laravel’s Form Requests with Bootstrap validation styling:
// app/Http/Requests/StoreUserRequest.php
public function rules() { return ['email' => 'required|email']; }
Blade template:
<x-input-error :messages="$errors->get('email')" class="text-danger" />
<input type="email" class="form-control @error('email') is-invalid @enderror">
Override Bootstrap variables in resources/scss/custom.scss:
$primary: #6f42c1;
@import "bootstrap/scss/bootstrap";
Compile via Laravel Mix.
Symfony vs. Laravel Ecosystem
{% bootstrap_button %} → <button class="btn">).Asset Paths
mix() or asset() helpers.npm install bootstrap.JavaScript Conflicts
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="{{ mix('js/bootstrap.bundle.min.js') }}"></script>
Bundle Registration
composer.json includes:
"autoload": {
"psr-4": {
"DontDrinkAndRoot\\BootstrapBundle\\": "vendor/dontdrinkandroot/bootstrap-bundle/src/"
}
}
composer dump-autoload after installation.{{ dd($variable) }} to inspect data structures.mix-manifest.json paths.php artisan cache:clear
php artisan config:clear
Custom Twig Extensions
If using Twig, extend the bundle’s BootstrapExtension:
// app/Providers/BootstrapServiceProvider.php
public function boot()
{
$twig = $this->app['twig'];
$twig->addExtension(new class extends \Twig\Extension\AbstractExtension {
public function getFunctions() {
return [
new \Twig\TwigFunction('custom_alert', [$this, 'renderAlert']),
];
}
public function renderAlert(string $type, string $message) {
return sprintf('<div class="alert alert-%s">%s</div>', $type, $message);
}
});
}
Laravel Livewire Integration Combine with Livewire for dynamic components:
<x-alert type="success" wire:model="successMessage"></x-alert>
// Livewire component
public $successMessage = "Saved!";
Dark Mode Use Bootstrap’s dark mode with Laravel’s session:
// postcss.config.js
module.exports = {
plugins: [
require('purgecss')({
content: ['resources/**/*.blade.php'],
defaultExtractor: content => content.match(/[\w-/:]+(?<!:)/g) || [],
}),
],
};
How can I help you explore Laravel packages today?