AlexMultisiteBundle - Branding and Internationalization
, (*1)
This bundle allows you to manage multiple brandings and multiple locales in a Symfony2 application., (*2)
Requirements:, (*3)
- FrameworkExtraBundle
- TwigBundle
Features
- Multiple routes for each site
- Configuration per site
- Templates per site
Installation
Add to your composer.json:, (*4)
{
"require": {
"alexandresalome/multisite-bundle": "~0.1"
}
}
Add the bundle to your kernel:, (*5)
# app/AppKernel.php
class AppKernel extends Kernel
{
public function registerBundles()
{
$bundles = array(
# ...
new Alex\MultisiteBundle\AlexMultisiteBundle(),
);
}
}
Configuration
Add this section to your config.yml file:, (*6)
alex_multisite:
default_branding: branding_A
default_locale: fr_FR
brandings:
_defaults:
register: true
branding_A:
en_GB: { host: branding-a.com }
fr_FR: { host: branding-a.com, prefix: /fr }
branding_B:
_defaults:
register: false
en_GB: { host: branding-b.com }
de_DE: { host: branding-b.de, register: false }
In this section, you must configure your brandings and locales., (*7)
You can also add extra options, like the register option here., (*8)
Declare your routes
In your controllers, substitute, (*9)
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
with, (*10)
use Alex\MultisiteBundle\Annotation\Route;
You can then configure a multisite route in two ways:, (*11)
/**
* @Route(name="login", paths={
* "fr_FR"="/connexion",
* "en_GB"="/login"
* })
*/
public function loginAction()
# ...
The path will be the same for all brandings, but will be localized. If you
want a different path for same locale in different sites:, (*12)
/**
* @Route(name="login", paths={
* "branding_A"={
* "fr_FR"="/connexion-on-A",
* "en_GB"="/login-on-A",
* },
* "branding_B"={
* "en_GB"="/login-on-B",
* },
* })
*/
public function loginAction()
# ...
Override templates
If you want to change a template for a specific site, create a similarly named file with branding/locale option in it:, (*13)
Given your default template is AcmeDemoBundle::contact.html.twig
., (*14)
You can override it with branding, locale, or both:, (*15)
AcmeDemoBundle::_branding_locale/contact.html.twig
AcmeDemoBundle::_branding_/contact.html.twig
AcmeDemoBundle::__locale/contact.html.twig
Just create the file and it will automatically be loaded in place of the previous one., (*16)
Read the site context
From templates, use the global variable site_context, which returns a Alex\MultisiteBundle\Branding\SiteContext
instance:, (*17)
You are currently on {{ site_context.currentBrandingName }}
Your locale is {{ site_context.currentLocale }}
You can also read options from config with:, (*18)
The option register is {{ site_context.option('register') ? 'enabled': 'not enabled' }}
In your controllers, use service site_context:, (*19)
public function indexAction()
{
$this->get('site_context')->getCurrentLocale();
$this->get('site_context')->getOption('register');
}
Disable route sorting
You might want to rely on natural order of routes. If you're in this case, you can disable the optimization by providing the sort_route option:, (*20)
alex_multisite:
sort_routes: false
# ...
Security
You can combine this bundle with Symfony's SecurityBundle, and use this bundle for your routes for login form page., (*21)
But, you must not use the multisite feature for the following routes:, (*22)