如何使用一个laravel安装来处理子域

时间:2015-12-16 10:30:04

标签: php laravel laravel-5 subdomain laravel-5.1

我正在创建一个laravel项目,我需要一个laravel安装,并在子域中使用单独数据库的实例。那些单独的数据库信息将不在config / database.php中。它将从master数据库获取,然后重新连接到另一个数据库。

我没有找到任何正确的方法来做到这一点。

你对此有什么想法吗?

感谢您的时间。

7 个答案:

答案 0 :(得分:7)

Laravel支持多个数据库连接。首先,在config/database.php中定义连接:

<?php
return array(

    'default' => 'default_connection',

    'connections' => array(

        // domain.com
        'default_connection' => array(
            'driver'    => 'mysql',
            'host'      => 'localhost',
            'database'  => 'primary_database',
            'username'  => 'username',
            'password'  => 'password'
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
        ),

        // sub.domain.com
        'subdomain_connection' => array(
            'driver'    => 'mysql',
            'host'      => 'localhost',
            'database'  => 'secondary_database',
            'username'  => 'username',
            'password'  => 'password'
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
        ),
    ),
);

现在要指定模型应该使用哪个连接,您可以在模型中设置$connection属性:

<?php

class YourModel extends Eloquent {

    protected $connection = 'subdomain_connection';

}

您可以以编程方式设置$connection的值。

答案 1 :(得分:5)

多租户是一个棘手的架构,需要谨慎建模。有几种方法可以实现这种架构。有些人决定使用单个数据库,而有些人则更喜欢使用多个数据库(在您的情况下)。

他们都有自己需要考虑的利弊。在开始建模应用程序之前,需要考虑很多因素。例如,子域的虚拟主机配置,数据库迁移(在需要时回滚所有数据库等)。我将建议这两个软件包可以帮助您前进,并让您更深入地了解如何为应用程序建模以满足您的需求。

https://github.com/orchestral/tenanti

https://github.com/hyn/multi-tenant

答案 2 :(得分:4)

我会这样做:

  • 每个域创建一个数据库

  • 在laravel config/database.php中设置可用的数据库连接:

'connections' => [

     'mysql_domain_1' => [
        'driver'    => 'mysql',
        /* other config values... */
    ],

    'mysql_domain_2' => [
        'driver'    => 'mysql',
        /* other config values... */
    ]
];
  • 在请求周期的早期阶段(例如在中间件中),从请求中获取子域,并相应地设置当前的DB连接

    例如,使用handle方法创建中间件:

public function handle($request, Closure $next)
{
    //check the request URL and get subdomain

    //get the db connection associated to the subdomain 

    //set the connection for this request
    Config::set('database.default', $dbConnection);
} 

Config::set('database.default', $dbConnection );将设置整个应用程序用于当前请求周期的数据库连接

答案 3 :(得分:3)

如果要从数据库处理此问题,请从http url检查主机名,并根据主机名从主表调用数据库连接。例如(http://abc.maindomain.com,从网址获取abc)

答案 4 :(得分:3)

您可以通过以下方式设置数据库配置:

$tenant = Tenant::whereSubDomain($subdomain)->first();  
Config::set('database.connections.mysql.database', $tenant->db_name);       
Config::set('database.connections.mysql.username',$tenant->db_username);
Config::set('database.connections.mysql.password',$tenant->db_password);

dd(\DB::connection('mysql'));

请参阅此链接Set up dynamic database connection on Multi tenant application以供参考。

答案 5 :(得分:0)

以下是我如何做到这一点:

config / database.php

<?php
function getDatabaseConnectionParameters() {
    $connectionParams = array();

    // add the default connection
    // this is your master database
    $connParams = array(
        'driver'    => 'mysql',
        'host'      => 'localhost',
        'database'  => 'master',
        'username'  => 'master_user',
        'password'  => 'master_password',
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => '',
        'strict'    => false,
    );
    array_push($connectionParams, array('mysql' => $connParams);

    // TODO: connect to your master database using PDO/mysqli or anything else you know.
    // The point is: you can't use Laravel ORM just yet because you are currently setting up its configuration!
    // Get the list of subdomain connection parameters and array_push it to $connectionParams just like above.
    // Example:
    // array_push($connectionParams, array('subdomain' => $subdomainConnParams)

    return $connectionParams;
}

return array (
    'default' => 'mysql'

    ,'connections' => getDatabaseConnectionParameters()
)
?>

有了这个,子域特定模型只需要正确指定$ connection。 例如:

<?php
class YourModel extends Eloquent {
    protected $connection = 'subdomain';
}
?>

这样,您的子域数据库配置可以保存在主数据库中,同时使您的模型简单且仍然是Laravel-ful。 此外,没有令人讨厌的黑客可能会使Laravel版本难以升级。

答案 6 :(得分:0)

偶然发现了这个问题,恕我直言,有时最简单的建议是最简单的。

我只需在/config/database.php文件的头部放置一个简单的开关:

switch($_SERVER['HTTP_HOST'])
{
case 'dev.yoursite.com':
    $selectedDatabase = 'mysite_dev';
    break;
case 'yoursite.com':
default:
    $selectedDatabase = 'mysite_live';
    break;
}

然后只需在返回的配置变量中使用该变量。

return [
    'connections' => 
        ['mysql' =>
             ['database' => $selectedDatabase,
              'username' => 'user_name',
              'password' => 'xxxxxxxxx',
             ],
        ]
    ];

我知道它不是laravel方式,但如果您只是想使用相同的PHP编码开辟一个快速测试环境,而不是数据库的测试实例,那么它将帮助您解决问题。