tahoelimited / multi-tenancy-bundle
Symfony 2 应用程序的多租户软件包
dev-master
2017-04-25 22:12 UTC
Requires
- php: >=5.5
- doctrine/doctrine-bundle: ~1.2
- doctrine/orm: ~2.2,>=2.2.3
- friendsofsymfony/user-bundle: ~2.0@dev
- 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-11 13:25:22 UTC
README
注意:该项目不再维护,并且与Symfony 3 不兼容。
安装
使用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);