CodeIgniter中的多租赁SaaS应用程序动态配置数据库
我正在尝试使用CodeIgniter4构建多个租赁SaaS应用程序。我想为每个Tanent的数据设置单个数据库,并为用户设置一个全局数据库。 我希望应用程序可以根据登录用户的塔尼特ID自动切换到Tanent的数据库。为此,我已经确定了塔nent子域,如下 app/config/startants.php
if(!defined('myHostName')){
$sd=explode(".",$_SERVER['HTTP_HOST']);
//define('myHostName', $host);
if($sd[0]=='localhost')
define('dbname', 'defaultdb');
else
define('dbname', $sd[0]);
这些代码识别子分类,并将dbname定义为子域的名称,如果呼叫来自子域。然后,我在App/Config/Database.php中定义了两个数据库组,如下所示。
public $default = [
'DSN' => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'db_blog',
'DBDriver' => 'MySQLi',
'DBPrefix' => '',
'pConnect' => false,
'DBDebug' => (ENVIRONMENT !== 'production'),
'charset' => 'utf8',
'DBCollat' => 'utf8_general_ci',
'swapPre' => '',
'encrypt' => false,
'compress' => false,
'strictOn' => false,
'failover' => [],
'port' => 3306,
];
public $data = [
'DSN' => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => myHostName,
'DBDriver' => 'MySQLi',
'DBPrefix' => '',
'pConnect' => false,
'DBDebug' => (ENVIRONMENT !== 'production'),
'charset' => 'utf8',
'DBCollat' => 'utf8_general_ci',
'swapPre' => '',
'encrypt' => false,
'compress' => false,
'strictOn' => false,
'failover' => [],
'port' => 3306,
];
这很好,但是现在我希望根据域上的文件夹动态选择数据库,例如 租户1 baseurl将为https://example.com/tenant1
租户2 baseurl将为https://example.com/tenant2
租户3 baseurl将为https://example.com/tenant3
这里的问题是第一部分(在这种情况下tenant1/tenant1/tenant2/tenant2/tenant3 )被确定为控制器由Codeigniter4,我认为必须有一些方法可以识别网站上的foldername url 或解决方案可能类似于
https://www.sandeeprajoria.in/2013/05/multi-tenancy-with-codeigniter.html
I am trying to build a multi tenancy saas Application using Codeigniter4. I want to setup individual database for each tanent's data and one global database for users.
I want application to automatically switch to tanent's database based on the tanent id of the logged in user. for this I have identified the tanents subdomain as follows inapp/config/constants.php
if(!defined('myHostName')){
$sd=explode(".",$_SERVER['HTTP_HOST']);
//define('myHostName', $host);
if($sd[0]=='localhost')
define('dbname', 'defaultdb');
else
define('dbname', $sd[0]);
These codes identify the subcomain and define the dbname as the name of subdomain if call is from subdomain. Then I have defined two database groups in app/config/database.php as follows.
public $default = [
'DSN' => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'db_blog',
'DBDriver' => 'MySQLi',
'DBPrefix' => '',
'pConnect' => false,
'DBDebug' => (ENVIRONMENT !== 'production'),
'charset' => 'utf8',
'DBCollat' => 'utf8_general_ci',
'swapPre' => '',
'encrypt' => false,
'compress' => false,
'strictOn' => false,
'failover' => [],
'port' => 3306,
];
public $data = [
'DSN' => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => myHostName,
'DBDriver' => 'MySQLi',
'DBPrefix' => '',
'pConnect' => false,
'DBDebug' => (ENVIRONMENT !== 'production'),
'charset' => 'utf8',
'DBCollat' => 'utf8_general_ci',
'swapPre' => '',
'encrypt' => false,
'compress' => false,
'strictOn' => false,
'failover' => [],
'port' => 3306,
];
This is working fine but Now I want the select database dynamically based on the folder on the domain e.g for
tenant 1 the baseurl will be https://example.com/tenant1
tenant 2 the baseurl will be https://example.com/tenant2
tenant 3 the baseurl will be https://example.com/tenant3
the problem here is that first segment (in this case tenant1/tenant2/tenant3
) are identified as controller by the codeigniter4, I assume that there must be some method to identify the foldername on sites url
or solution may be similar to https://www.sandeeprajoria.in/2013/05/multi-tenancy-with-codeigniter.html
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
'https://stackoverflow.com/questions/65790279/how-to-to-set-dynamic-baseurl-on-an-app-config-config-php-php-php- in-codeigniter-4'向我展示了如何回答我的问题。
$ baseUrl是前缀所有URL的常数,如果我动态更改它,我可以在startants.php文件中访问此变量。由于我无法在database.php中创建或修改变量,因此我在stonstants.php中创建了一个常数,并动态分配了用于该常数特定租户的数据库的名称。常量是常量。php中定义的常数。
最喜欢实现类似解决方案的任何更好的想法。
'https://stackoverflow.com/questions/65790279/how-to-set-dynamic-baseurl-on-app-config-app-php-using-codeigniter-4' shown me how to answer to my problem.
$baseURL is the constant that is prefixed to all the urls and if I change it dynamically I can access this variable in Constants.php file. Since I can not create or modify variables in database.php I created a constant in constants.php and dynamically assigned the name of database to be used for a specific tenant to that constant. constants defined in Constants.php are available in database.php so I could dynamically configure the database for specific tenant.
Any better idea to achieve similar solution is most welcome.