The repository contains the Symfony multisite bundle for Symfony 3. This bundle allows you to manage multisite in symfony application.
- Each site can have its own database connection or use the same database.
- Each site can have diffirent locales
- Each site can have its own app folder and public folder
- Each site can have its own robots.txt
Each sites first looks for assets in its own folder
For example (demo_2):
The site demo_2 will first look for the image chat.png in
/web/public/demo_2/imgs/chat.png
if this image does not exist, the file /web/imgs/chat.png will be used
Each site can have its own robots.txt in the folder :
/robots/hostname.txt
if hostname.txt does not exist, the file /web/robots.txt will be used.
composer require prodigious/symfony-multisite-bundle
$bundles = array(
...
new Prodigious\MultisiteBundle\MultisiteBundle(),
...
);
Then add these lines to your composer.json of your Symfony project:
"scripts": {
"post-install-cmd": [
...
"Prodigious\\MultisiteBundle\\Composer\\ScriptHandler::installBundle"
],
"post-update-cmd": [
...
"Prodigious\\MultisiteBundle\\Composer\\ScriptHandler::installBundle"
]
}
Afterwards, initialize the bundle using
composer install
After the installation, the bundle will create some files in your project :
- sites/
- app/MultisiteKernel.php
And you need to modify some files :
<IfModule mod_rewrite.c>
RewriteEngine On
# Multisite conditions
RewriteCond %{DOCUMENT_ROOT}/robots/%{HTTP_HOST}.txt -f
RewriteRule ^robots\.txt$ robots/%{HTTP_HOST}.txt [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^public/([^/]+)(?:/(.*)|$) /$2 [QSA,R,L]
# End
</IfModule>
Replace
require __DIR__.'/../vendor/autoload.php
To
require __DIR__.'/../sites/autoload/sites.php';
And Relace
// app.php
$kernel = new AppKernel('prod', false);
// app_dev.php
$kernel = new AppKernel('dev', true);
To
// app.php
$kernel = new MultisiteKernel('prod', false);
$kernel->setSite($site);
// app_dev.php
$kernel = new MultisiteKernel('dev', true);
$kernel->setSite($site);
Like this :
// require __DIR__.'/../vendor/autoload.php';
require __DIR__.'/../sites/autoload/sites.php';
if (PHP_VERSION_ID < 70000) {
include_once __DIR__.'/../var/bootstrap.php.cache';
}
$kernel = new MultisiteKernel('prod', false);
$kernel->setSite($site);
if (PHP_VERSION_ID < 70000) {
$kernel->loadClassCache();
}
There is a list of commands to manage your sites.
php bin/console site:create
php bin/console site:list
php bin/console site:disable --name=demo_1
php bin/console site:enable --name=demo_1
php bin/console site:delete --name=demo_1
php bin/console site:config:sync
$currentSite = $request->attributes->get('site'); // 'app' by default
$currentInstance = $request->attributes->get('instance');
$currentLocal = $request->getLocale();
Author: Nan GUO
Company : Prodigious