Installation:
composer require sgk/barcode-bundle:~2.0
Register the bundle in config/app.php (Laravel) or AppKernel.php (Symfony):
// Laravel (Service Provider)
$provider = new \SGK\BarcodeBundle\BarcodeServiceProvider();
$this->registerServiceProviders([$provider]);
First Use Case: Generate a simple EAN-13 barcode in Twig (Blade):
{!! barcode('ean13', '1234567890128') !!}
Or in PHP (Laravel):
use SGK\BarcodeBundle\Generator\BarcodeGenerator;
$generator = app(BarcodeGenerator::class);
$barcode = $generator->generate('ean13', '1234567890128', 'svg');
Where to Look First:
vendor/sgk/barcode-bundle/Resources/doc/ for examples.vendor/sgk/barcode-bundle/Generator/BarcodeGenerator.php for API details.vendor/sgk/barcode-bundle/Twig/BarcodeExtension.php for template usage.Twig/Blade Integration:
Use the barcode() function in templates for dynamic generation:
{!! barcode('ean13', product.sku, {
'format': 'png',
'scale': 2,
'textPosition': 'bottom'
}) !!}
return view('product.show', ['sku' => $product->sku]);
Controller-Based Generation: Generate barcodes programmatically for APIs or dynamic responses:
public function generateBarcode(Request $request)
{
$generator = app(BarcodeGenerator::class);
$barcode = $generator->generate(
$request->type,
$request->data,
$request->format ?? 'svg'
);
return response($barcode, 200)->header('Content-Type', 'image/svg+xml');
}
Storage and Retrieval:
Save generated barcodes to storage (e.g., storage/app/barcodes/):
$path = $generator->generateToFile('ean13', '1234567890128', 'png', 'barcodes/');
return asset($path); // Return URL for frontend
Batch Processing: Generate multiple barcodes in a loop (e.g., for bulk exports):
foreach ($products as $product) {
$generator->generateToFile('ean13', $product->sku, 'png', "barcodes/{$product->id}/");
}
barcode() function in Vue/React components for dynamic UI updates.GenerateBarcodeJob::dispatch($type, $data, $format, $path);
$cacheKey = "barcode:{$type}:{$data}";
$barcode = cache()->remember($cacheKey, now()->addHours(1), function() use ($generator, $type, $data) {
return $generator->generate($type, $data, 'svg');
});
Symfony vs. Laravel:
BarcodeServiceProvider and BarcodeFacade (if using).class BarcodeServiceProvider extends ServiceProvider {
public function register() {
$this->app->bind('barcode', function() {
return new \SGK\BarcodeBundle\Generator\BarcodeGenerator();
});
}
}
Twig Extension Conflicts:
Blade::directive('barcode', function($expression) {
return "<?php echo app('barcode')->generate($expression); ?>";
});
Usage:
@barcode('ean13', '1234567890128')
File Permissions:
generateToFile() requires writable directories. Use Laravel’s storage_path():
$path = $generator->generateToFile('ean13', '1234567890128', 'png', storage_path('app/barcodes/'));
Unsupported Types:
codabar, pharmacode).SVG Output Quirks:
format: 'png' for consistency in critical applications.if (!\SGK\BarcodeBundle\Validator\BarcodeValidator::isValid($type, $data)) {
throw new \InvalidArgumentException("Invalid {$type} data");
}
$raw = $generator->generate('ean13', '1234567890128', 'svg');
file_put_contents('debug.svg', $raw); // Save for inspection
Custom Formats: Extend the generator to support additional formats (e.g., PDF via Dompdf):
class ExtendedBarcodeGenerator extends \SGK\BarcodeBundle\Generator\BarcodeGenerator {
public function generatePdf($type, $data) {
$svg = parent::generate($type, $data, 'svg');
// Convert SVG to PDF using Dompdf
}
}
Dynamic Options: Override default options (e.g., colors, margins) via config:
// config/barcode.php
'defaults' => [
'format' => 'svg',
'scale' => 2,
'foreground' => '#000000',
'background' => '#FFFFFF',
],
Event Listeners: Hook into barcode generation events (if the bundle supports them) or wrap the generator in a decorator pattern:
class LoggingBarcodeGenerator {
protected $generator;
public function __construct(BarcodeGenerator $generator) {
$this->generator = $generator;
}
public function generate($type, $data, $format) {
\Log::info("Generating {$type} barcode for data: {$data}");
return $this->generator->generate($type, $data, $format);
}
}
Localization:
Add support for non-Latin characters in barcodes (e.g., code128 with Unicode):
$generator->generate('code128', '日本語', 'svg', ['textPosition' => 'bottom']);
How can I help you explore Laravel packages today?