Installation:
composer require bushidoio/qrcode-bundle
Add the bundle to config/bundles.php:
return [
// ...
BushidoIO\QRCodeBundle\BushidoIOQRCodeBundle::class => ['all' => true],
];
Enable Routes:
Ensure config/routes.yaml includes:
bushidoio_qrcode:
resource: "@BushidoIOQRCodeBundle/Controller/"
type: annotation
First Use Case: Generate a QR code in Twig:
{{ 'https://example.com'|qrcode }}
Outputs a <img> tag with the QR code.
src/Resources/views/ for Twig templates and DependencyInjection/ for service definitions.Resources/config/services.yaml; override in config/packages/bushidoio_qrcode.yaml.Controller/QRCodeController.php for direct API usage.Twig Integration:
|qrcode filter for URLs or |qrcode_base64 for inline images:
<img src="{{ 'data:text/plain;base64,' ~ 'SGVsbG8='|qrcode_base64 }}" />
{{ 'text'|qrcode({ size: 200, mask: 'best' }) }}
API Endpoints:
curl "http://app.dev/qrcode?text=Hello&size=300&format=png"
Cache-Control headers (configurable via cache_expiration).Symfony Services:
BushidoIO\QRCodeBundle\Service\QRCodeService for programmatic use:
$service->generate('https://example.com', ['size' => 500]);
Cache Optimization:
cache_dir).cache_expiration (e.g., 3600 for 1 hour) to reduce CPU load.Dynamic Content: Use Twig in controllers to generate QR codes for user-specific data:
return $this->render('page.html.twig', [
'user_qr' => $user->email . '|' . $user->id,
]);
{{ user_qr|qrcode }}
Asset Pipeline: For static QR codes, pre-generate and reference via assets:install:
php bin/console bushidoio:qrcode:generate --text="StaticText" --output=public/qr/
Testing: Mock QRCodeService in PHPUnit:
$this->createMock(QRCodeService::class)
->method('generate')
->willReturn('<img src="mock.png" />');
Cache Directory Permissions:
cache_dir (default: var/cache/qrcode/) is writable by the web server.touch var/cache/qrcode/ and check permissions (chmod -R 775 var/cache/).Image Size Limits:
max_size (1024px) may cause distortion for large text. Override in config:
bushidoio_qrcode:
max_size: 2048
Twig Filter Scope:
|qrcode filter requires the bundle’s Twig extension. If missing, clear cache:
php bin/console cache:clear
HTTPS Mixed Content:
cache_expiration_ssl separately:
bushidoio_qrcode:
cache_expiration: 3600
cache_expiration_ssl: 86400
APP_DEBUG=true) and check var/log/dev.log for QRCodeBundle errors.cache_dir permissions or max_size constraints.routing.yml is loaded and the bushidoio_qrcode route is enabled.Custom Masks:
QRCodeService:
// src/Service/CustomQRCodeService.php
class CustomQRCodeService extends \BushidoIO\QRCodeBundle\Service\QRCodeService {
protected function getMask() { return 'custom_mask'; }
}
config/services.yaml:
services:
App\Service\CustomQRCodeService: ~
New Formats:
QRCodeController and updating the generate method.Event Listeners:
kernel.request):
public function onKernelRequest(GetResponseEvent $event) {
if ($event->getRequest()->query->has('qrcode')) {
// Modify generation logic
}
}
Batch Generation: Use Symfony’s Messenger component to queue QR code generation for background processing:
$this->messageBus->dispatch(new GenerateQRCodeMessage($text, $outputPath));
Analytics: Track QR code usage by appending UTM parameters:
{{ ('https://example.com?utm_source=qrcode')|qrcode }}
Theming: Override Twig templates in templates/bundles/bushidoioqrcode/ to customize the <img> tag (e.g., add class or data-* attributes).
How can I help you explore Laravel packages today?