承接 ludovicm67/laravel-multidomain 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

ludovicm67/laravel-multidomain

Composer 安装命令:

composer require ludovicm67/laravel-multidomain

包简介

Dynamic Laravel configuration depending of the domain

README 文档

README

Let's get started

First of all, include this library into your Laravel projet dependencies, using the following command:

composer require ludovicm67/laravel-multidomain

Create a file called config.yaml at the root of your project, with something like:

fallback_url: http://localhost/
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

If the current hostname is not in the supported_domains list, the app will redirect to the fallback_url.

One special case: if the asked domain is starting with api. and if in the configuration file there are only a version without the api., this last one will be used.

You can add all properties as you wish; here we will for example see how to have a different database for each domain.

To get started, update the bootstrap/app.php file to load the configuration, as follow:

<?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.yaml')
);

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

If you want to have access to some properties, you can now add everywhere you want the following:

<?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

In our example, where we wanted to have a specific database configuration for each domain, you just have to update your config/database.php file, to make it similar to something like:

<?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;

And to run migrations, just run the following command:

php artisan migrate --database=YOUR_DOMAIN

In our example, it will be:

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

And that's it! 😉

ludovicm67/laravel-multidomain 适用场景与选型建议

ludovicm67/laravel-multidomain 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 125 次下载、GitHub Stars 达 4, 最近一次更新时间为 2018 年 07 月 13 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 ludovicm67/laravel-multidomain 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 ludovicm67/laravel-multidomain 我们能提供哪些服务?
定制开发 / 二次开发

基于 ludovicm67/laravel-multidomain 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 125
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 4
  • 点击次数: 6
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 4
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-07-13