alexandresalome / multisite-bundle
此包已被弃用且不再维护。没有建议的替代包。
用于管理具有不同品牌和地域的多个站点的包
v0.2.0
2016-07-09 11:12 UTC
Requires
- sensio/framework-extra-bundle: ~3.0
- symfony/symfony: ~2.4|~3.0
This package is auto-updated.
Last update: 2023-10-27 19:52:38 UTC
README
此包允许您在 Symfony2 应用程序中管理多个品牌和多个地域。
需求:
- FrameworkExtraBundle
- TwigBundle
功能
- 每个站点的多个路由
- 按站点配置
- 按站点模板
安装
将其添加到您的 composer.json
{ "require": { "alexandresalome/multisite-bundle": "~0.1" } }
将包添加到您的内核
# app/AppKernel.php class AppKernel extends Kernel { public function registerBundles() { $bundles = array( # ... new Alex\MultisiteBundle\AlexMultisiteBundle(), ); } }
配置
将此部分添加到您的 config.yml 文件中
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 }
在此部分中,您必须配置您的品牌和地域。
您还可以添加额外选项,例如这里的 register 选项。
声明您的路由
在您的控制器中,用以下内容替换
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
用以下内容替换
use Alex\MultisiteBundle\Annotation\Route;
您可以通过两种方式配置多站路由
/** * @Route(name="login", paths={ * "fr_FR"="/connexion", * "en_GB"="/login" * }) */ public function loginAction() # ...
路径对于所有品牌都是相同的,但将本地化。如果您希望在不同站点中对于同一地域有不同路径
/** * @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() # ...
覆盖模板
如果您想为特定站点更改模板,创建一个具有品牌/地域选项的类似命名的文件
假设您的默认模板是 AcmeDemoBundle::contact.html.twig
。
您可以使用品牌、地域或两者来覆盖它
AcmeDemoBundle::_branding_locale/contact.html.twig
AcmeDemoBundle::_branding_/contact.html.twig
AcmeDemoBundle::__locale/contact.html.twig
只需创建文件,它将自动替换之前的文件。
读取站点上下文
从模板中,使用全局变量 site_context,它返回一个 Alex\MultisiteBundle\Branding\SiteContext
实例
You are currently on {{ site_context.currentBrandingName }}
Your locale is {{ site_context.currentLocale }}
您还可以使用以下方式从配置中读取选项
The option register is {{ site_context.option('register') ? 'enabled': 'not enabled' }}
在控制器中,使用服务 site_context
public function indexAction() { $this->get('site_context')->getCurrentLocale(); $this->get('site_context')->getOption('register'); }
禁用路由排序
您可能希望依赖于路由的自然顺序。如果您处于这种情况,您可以通过提供 sort_route 选项来禁用优化
alex_multisite: sort_routes: false # ...
安全
您可以将此包与 Symfony 的 SecurityBundle 结合使用,并使用此包为您的登录表单页面路由
但是,您**不能**在以下路由中使用多站功能
- login-check
- logout