adipso / laravel-multidomain

2.0 2023-03-10 15:00 UTC

This package is auto-updated.

Last update: 2024-09-10 18:30:41 UTC


README

让我们开始吧

首先,使用以下命令将此库包含到您的Laravel项目依赖中:

composer require ludovicm67/laravel-multidomain`

在项目根目录下创建一个名为 config.yml 的文件,内容如下:

fallback_url: https:///
supported_domains:
  localhost:
    site_name: Localhost
    database:
      hostname: localhost
      username: root
      password:
      database: db
  amazing.localhost:
    site_name: Amazing!
    database:
      hostname: localhost
      username: amazing
      password: wow
      database: amazing

如果当前主机名不在 supported_domains 列表中,应用程序将重定向到 fallback_url

一个特殊情况:如果请求的域名以 api. 开头,并且在配置文件中只有不带 api. 的版本,则将使用最后一个版本。

您可以添加所有您想要的属性;例如,我们将展示如何为每个域名配置不同的数据库。

要开始,更新 bootstrap/app.php 文件以加载配置,如下所示:

<?php

// create the application
$app = new Illuminate\Foundation\Application(
    realpath(__DIR__.'/../')
);

// load all required configuration for multi domain support
\ludovicm67\Laravel\Multidomain\Configuration::getInstance(
    base_path('config.yml')
);

// ... the rest of the file will be the same so keep it

如果您想要访问某些属性,现在您可以在任何需要的地方添加以下内容:

<?php
// ...
use \ludovicm67\Laravel\Multidomain\Configuration;

// ...

$config = Configuration::getInstance(); // here we will get our instance
$config->get(); // to get access to the whole configuration
$config->domain(); // to get access to the current domain configuration

在我们的示例中,我们想要为每个域名有特定的数据库配置,只需更新您的 config/database.php 文件,使其类似如下:

<?php

use \ludovicm67\Laravel\Multidomain\Configuration;
use \ludovicm67\Laravel\Multidomain\ConfigurationObject;

// default configuration without all comments
$databaseConfiguration = [
  'default' => env('DB_CONNECTION', 'mysql'),
  // removed 'connections' key here
  'migrations' => 'migrations',
  'redis' => [
    'client' => 'predis',
    'default' => [
      'host' => env('REDIS_HOST', '127.0.0.1'),
      'password' => env('REDIS_PASSWORD', null),
      'port' => env('REDIS_PORT', 6379),
      'database' => 0,
    ],
  ],
];

// get configuration
$config = Configuration::getInstance();
$globalConf = $config->get();
$domainConf = $config->getDomain();
$databaseConfiguration['connections'] = []; // empty array
$databaseConfiguration['connections']['mysql'] = [
  'driver' => 'mysql',
  'database' => ''
]; // default to prevent some errors

// add default database connection if we have a domain
if (!is_null($domainConf)) {
  $databaseConf = $domainConf->get('database');
  if (!is_null($databaseConf) && is_object($databaseConf)) {
    // we create the default database connection using our specified domain
    $databaseConfiguration['connections']['mysql'] = [
      'driver' => 'mysql',
      'host' => $databaseConf->get('hostname'),
      'port' => '3306',
      'database' => $databaseConf->get('database'),
      'username' => $databaseConf->get('username'),
      'password' => $databaseConf->get('password'),
      'unix_socket' => env('DB_SOCKET', ''),
      'charset' => 'utf8mb4',
      'collation' => 'utf8mb4_unicode_ci',
      'prefix' => '',
      'strict' => true,
      'engine' => null,
    ];
  }
}

// append database configuration for other domains (for migrations for example)
$supportedDomains = $globalConf->get('supported_domains');
if (!empty($supportedDomains)) $supportedDomains = $supportedDomains->get();
if (!empty($supportedDomains)) {
  foreach ($supportedDomains as $domain => $conf) {
    $databaseConf = (new ConfigurationObject($conf))->get('database');
    if (!is_null($databaseConf) && is_object($databaseConf)) {
      $databaseConfiguration['connections'][$domain] = [
        'driver' => 'mysql',
        'host' => $databaseConf->get('hostname'),
        'port' => '3306',
        'database' => $databaseConf->get('database'),
        'username' => $databaseConf->get('username'),
        'password' => $databaseConf->get('password'),
        'unix_socket' => env('DB_SOCKET', ''),
        'charset' => 'utf8mb4',
        'collation' => 'utf8mb4_unicode_ci',
        'prefix' => '',
        'strict' => true,
        'engine' => null,
      ];
    }
  }
}

return $databaseConfiguration;

要运行迁移,只需运行以下命令:

php artisan migrate --database=YOUR_DOMAIN

在我们的示例中,将如下所示:

php artisan migrate --database=localhost
php artisan migrate --database=amazing.localhost

就这样! 😉