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


How can I handle subdomains with one laravel installation

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

我没有找到任何合适的方法来做这件事。

你对此有什么想法吗?

谢谢你抽出时间。

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的值。

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

他们都有自己的优点和缺点,你需要考虑。在开始对应用程序建模之前,需要考虑很多因素。例如子域的虚拟主机配置,数据库迁移(在需要时回滚所有数据库,等等)。我会建议这两个包,它们可以帮助你,并让你更深入地了解如何为你的应用程序建模,以适应你想要的。

https://github.com/orchestral/tenanti

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

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

我会这样做:

  • 为每个域创建一个数据库

  • 在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 );将为当前请求周期设置整个应用程序使用的数据库连接

刚刚偶然发现了这个问题,IMHO有时最简单的建议是最简单的。

我只是在/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',
             ],
        ]
    ];

我知道这不是一种简单的方法,但如果你只想打开一个快速测试环境,使用相同的PHP代码,但使用数据库的测试实例,它会让你摆脱困境。

您可以通过以下操作设置DB配置:

$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'));

请参阅此链接在多租户应用程序上设置动态数据库连接以供参考。

以下是我如何处理这个问题:

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版本变得困难。