syonix / multi-tenancy-bundle
适用于symfony 2应用的多个租户包
v1.0.1
2016-06-07 13:50 UTC
Requires
- php: >=5.5
- doctrine/doctrine-bundle: ~1.2
- doctrine/orm: ~2.2,>=2.2.3
- friendsofsymfony/user-bundle: ~2.0
- recurly/recurly-client: 2.3.*
- symfony/form: ~2.3
- symfony/framework-bundle: ~2.3
- symfony/security-bundle: ~2.3
- symfony/twig-bundle: ~2.3
This package is not auto-updated.
Last update: 2024-09-14 18:58:44 UTC
README
安装
使用Composer下载MultiTenancyBundle
运行以下命令添加MultiTenancy
$ php composer.phar require "tahoelimited/multi-tenancy-bundle": "dev-master"
Composer会将该包安装到项目的vendor/tahoelimited
目录中。
启用包
在kernel中启用包
<?php // app/AppKernel.php public function registerBundles() { $bundles = array( // ... new Tahoe\Bundle\MultiTenancyBundle\TahoeMultiTenancyBundle(), ); }
配置包
将以下设置添加到您的config.yml
中,您必须保留现有值,不要简单地覆盖整个doctrine
parameters.yml
parameters: tahoe_multi_tenancy.user.class: Tahoe\ExampleBundle\Entity\User tahoe_multi_tenancy.tenant.class: Tahoe\ExampleBundle\Entity\Tenant domain: yourdomain.com
config.yml
doctrine: orm: resolve_target_entities: Tahoe\Bundle\MultiTenancyBundle\Model\MultiTenantUserInterface: %tahoe_multi_tenancy.user.class% Tahoe\Bundle\MultiTenancyBundle\Model\MultiTenantTenantInterface: %tahoe_multi_tenancy.tenant.class% entity_managers: default: filters: tenantAware: class: Tahoe\Bundle\MultiTenancyBundle\Query\Filter\SQLFilter\TenantAwareFilter enabled: true fos_user: registration: form: type: tahoe_multitenancy_user_registration tahoe_multi_tenancy: subdomain_strategy: tenant_aware # one of tenant_aware or fixed account_prefix: YOUR_ACCOUNT_PREFIX # optional, if you want to override the registration subscriber registration_subscriber: class: Your\Full\Domain\Class manager: your.manager.service # if you need a manager in the subscriber gateways: # for the moment, only recurly is supported recurly: subdomain: your-subdomain private_key: YOUR_PRIVATE_KEY plan_name: YOUR_PLAN_NAME
如果您使用的是tenant_aware
子域策略,则租户将通过他们选择的子域访问APP。如果您选择fixed
,则所有租户将通过相同的端点访问,租户将被存储在登录用户中(而不是由子域解析)。
注意:您可以使用自己的表单类型以及registration_subscriber
来获得更强大的行为。
routing.yml
tahoe_multi_tenancy: resource: "@TahoeMultiTenancyBundle/Resources/config/routing.yml" prefix: /
创建自己的租户实体
您必须在包内部创建租户实体,它扩展了包提供的实体。例如,如下所示
<?php namespace Tahoe\ExampleBundle\Entity; use Tahoe\Bundle\MultiTenancyBundle\Entity\Tenant as BaseTenant; use Tahoe\Bundle\MultiTenancyBundle\Model\MultiTenantTenantInterface; class Tenant extends BaseTenant implements MultiTenantTenantInterface { // your custom properties and methods }
# file is extending base tenant from multi tenancy bundle Tahoe\ExampleBundle\Entity\Tenant: type: entity table: th_ex_tenant fields: # your custom fields
更新您的现有用户实体。
注意:MultiTenancyBundle需要FOSUSERBundle。
<?php namespace Tahoe\ExampleBundle\Entity; use FOS\UserBundle\Model\User as BaseUser; use Tahoe\Bundle\MultiTenancyBundle\Model\MultiTenantUserInterface; class User extends BaseUser implements MultiTenantUserInterface { protected $id; protected $activeTenant; public function __construct() { parent::__construct(); } /** * @return mixed */ public function getId() { return $this->id; } /** * @return mixed */ public function getActiveTenant() { return $this->activeTenant; } /** * @param mixed $activeTenant * * @return $this */ public function setActiveTenant($activeTenant) { $this->activeTenant = $activeTenant; return $this; } }
Tahoe\ExampleBundle\Entity\User: type: entity table: th_ex_user repositoryClass: Tahoe\ExampleBundle\Repository\UserRepository id: id: type: integer generator: strategy: AUTO
使其他实体租户感知
所有特定于租户的实体都应该应用以下内容。所有适用于所有租户的实体应保持不变。
<?php namespace Tahoe\ExampleBundle\Entity; use Tahoe\Bundle\MultiTenancyBundle\Model\TenantAwareInterface; use Tahoe\Bundle\MultiTenancyBundle\Model\TenantTrait; class Customer implements TenantAwareInterface { use TenantTrait; }
Tahoe\ExampleBundle\Entity\Customer: type: entity table: th_ex_customer fields: id: type: integer id: true generator: strategy: AUTO name: type: string manyToOne: tenant: targetEntity: Tenant
确保/是自由的
/用于将租户重定向,因此您不能设置仅包含/的路由
使用包服务
您可以使用此包提供的服务来获取连接的租户。
当使用此包的命令行时,您可能会发现tenantId
未设置。为了避免此错误,您需要手动设置要与之工作的租户,通过调用TenantResolver
类的overrideTenant
。
$this->getContainer()->get('tahoe.multi_tenancy.tenant_resolver')->overrideTenant($tenant);