adespresso/white-label-bundle
Symfony2 bundle for shipping white-labeled versions of a website. Configure multiple “websites” selected by domain/host or user parameters, with priorities and custom params. Provides Twig helpers: whitelabel conditional blocks, website() info, and impersonateUrl().
Installation:
composer require adespresso/white-label-bundle
Add the bundle to AppKernel.php:
new Ae\WhiteLabelBundle\AeWhiteLabelBundle(),
Basic Configuration:
Define a config.yml (or packages/adespresso/white-label-bundle/white_label.yml in Symfony 5+) with a single white-label:
ae_white_label:
default_website: default_site
websites:
default_site:
label: "Default Label"
host: "*.example.com" # Wildcard for subdomains
method: byHost
priority: 1
First Use Case:
Use the whitelabel Twig function to conditionally render content:
{% if whitelabel('default_site') %}
<h1>Welcome to the Default Site</h1>
{% else %}
<h1>Welcome to the Generic Site</h1>
{% endif %}
Domain-Based Routing:
byHost method to route traffic based on the HTTP Host header.foo.domain.com maps to foo_site in config.router.request_context to dynamically adjust routes or assets.User-Based Impersonation:
byUserParam to switch labels based on a user entity attribute (e.g., user.origin).user_param:
key: origin
value: "bar_website"
Ae\WhiteLabelBundle\Event\WhiteLabelEvent to fetch user data in event listeners.Dynamic Asset Loading:
website function:
<link rel="stylesheet" href="{{ asset(website('default_site').css_path ~ 'styles.css') }}">
custom_params:
custom_params:
css_path: "/bundles/foo_site/css/"
URL Generation:
impersonateUrl() to generate URLs for other white-labels:
<a href="{{ path('home', {'_white_label': 'bar_site'}) }}">Switch to Bar Site</a>
UrlGenerator to respect _white_label parameter.Event-Driven Extensions:
ae_white_label.switch event to modify behavior dynamically:
// services.yaml
Ae\WhiteLabelBundle\EventListener\WhiteLabelSubscriber:
tags:
- { name: kernel.event_listener, event: ae_white_label.switch, method: onWhiteLabelSwitch }
Database-Driven Labels:
white_label_configs).ae_white_label.websites in config.yml:
websites: "@=service('app.white_label_loader').load()"
WhiteLabelLoader to query your DB.Multi-Tenant SaaS:
// In a repository
$this->setWhiteLabel($whiteLabelId); // Hypothetical method
Ae\WhiteLabelBundle\WhiteLabelContext service to access the current label.Host Matching Quirks:
byHost uses exact matches by default. For subdomains, use wildcards (*.domain.com).host values in config match the incoming Host header exactly or use regex via a custom WhiteLabelResolver.Priority Conflicts:
byHost and byUserParam), the highest priority wins.dump(website()) in Twig to inspect the resolved label.Twig Function Scope:
whitelabel(), website(), and impersonateUrl() only work in Twig templates. For PHP logic, use the WhiteLabelContext service:
$currentLabel = $this->get('ae_white_label.context')->getCurrentWebsite();
Caching Issues:
Cache-Control: private headers._white_label to cache keys manually:
$key = 'my_cache_' . $this->get('ae_white_label.context')->getCurrentWebsite();
Deprecated Symfony 2:
config/bundles.php).{{ bundle('AeWhiteLabelBundle') }} if needed).Enable Debug Mode:
Add this to config.yml to log resolved labels:
ae_white_label:
debug: true
Check var/log/dev.log for entries like:
[WhiteLabel] Resolved 'foo_site' via 'byHost' (priority: 1)
Test Host Overrides:
Use Symfony’s request_context to simulate different hosts in tests:
$client->getContainer()->get('router.request_context')->setHost('foo.domain.com');
Validate Config: Run:
php bin/console debug:config ae_white_label
To ensure no syntax errors in YAML.
Custom Resolvers:
Ae\WhiteLabelBundle\Resolver\WhiteLabelResolverInterface for new matching methods (e.g., byHeader for custom HTTP headers).ae_white_label.resolver.Twig Extensions:
Ae\WhiteLabelBundle\Twig\WhiteLabelExtension service.Database Backend:
Ae\WhiteLabelBundle\Loader\WhiteLabelLoaderInterface.Asset Pipeline:
custom_params:
asset_manifest: "assets/foo_site.json"
How can I help you explore Laravel packages today?