democracyapps/domain-context 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

democracyapps/domain-context

Composer 安装命令:

composer require democracyapps/domain-context

包简介

Utility to facilitate mapping parts of a route hierarchy to separate domains

README 文档

README

This is a simple Laravel utility to facilitate mapping parts of a route hierarchy to separate domains.

As an example, let's say that your company has a site called coolmarketplace.com which lets other companies sign up to list their products. In addition to any pages you provide to highlight interesting or popular products, each company also has its own page, identified by a short-code that they pick. For example, Golly Gee Widgets has the page http://coolmarketplace.com/golly.

However, they also want that mini-site to show up under their own domain name, http://market.gollygee.com, probably with different styling, links, etc. You will need to have different application logic or presentation for the same mini-site depending on whether it's accessed as coolmarketplace.com/golly or market.gollygee.com.

The utility creates a singleton DomainContext object that can be used to test whether the current request is coming in via a domain that should be mapped differently from the default and to get the identifier for the internal object that is associated with that domain.

Instructions For Use

Installation

Begin by installing this package through Composer.

{
    "require": {
        "democracyapps/domain-context": "dev-master"
    }
}

Add the service provider to app.php

// app/config/app.php

'providers' => [
    '...',
    'DemocracyApps\DomainContext\DomainContextServiceProvider',
];

For convenience, you may also add the Facade to app.php

'aliases' => [
    '...',
    'DomainContext' => 'DemocracyApps\DomainContext\DomainContextFacade',
    ];

Configuration Parameters

Next, publish the configuration file by running

php artisan vendor:publish

and edit 'config/domain-context.php'. There are currently four configuration settings in use in the package.

mapped_domain_storage (default: 'config')

You may specify domains that are to be mapped in either the configuration file or in a database table. This parameter determines which is to be used. By default, mapped domains are simply added to the configuration file itself (see mapped_domains parameter). If mapped_domain_storage is set to 'database', then it will look in the table specified by the mapped_domain_table_name parameter.

mapped_domains (default: empty array)

If mapped_domain_storage is set to 'config', then this array provides a list of the domains being mapped and the identifiers associated with them. The identifiers may be of any type. In the example below, http://market.gollygee.com is a mapped domain and is associated internally with a short code of 'golly'. A more common use might be to associate it with an ID of a database model, as in the second line.

    'mapped_domains' => [
        'market.gollygee.com' => 'golly',
        'stdexample.com' => 133
    ]

mapped_domain_table_name (default: 'mapped_domains')

If mapped_domain_storage is set to 'database', information on mapped domains is searched in the table identified by this parameter. It expects the table to have a 'domain_name' column and an 'identifier' column. It doesn't do anything with them - simply returns them when requested (see below) and interprets a match of the current domain with a table entry as indicating that the domain is mapped.

view_variable_name (default: 'domainContext')

The DomainContext object is automatically made available to all Laravel views via, by default, the $domainContext variable. This parameter can be used to specify a different variable name.

How To Use

Let's take the example at the top of this readme.

There are three common places where you'll actually implement variations in the logic or presentation in response to the incoming domain: the routes.php file, controllers, and views. The logic is your own, of course. You just need an easy way to tell whether you're in a mapped domain or not and, if so, what the associated client object should be.

The DomainContext object provides this via three methods: DomainContext::isMapped(), DomainContext::getDomain(), and DomainContext::getDomainIdentifier() (of course, you can also get the object via app()->make).

The first is a boolean that indicates whether the current domain is one that has been mapped. The second tells you what the domain is (in the example above, it would return 'market.gollygee.com'). The last returns the identifier to be used, e.g., to load client information from a table ('golly' in first example above).

To use them in the routes file, the simplest thing to do is use the facade. For example:

if (\DomainContext::isMapped()) {
    require app_path().'/Http/Routes/mapped.php'; // all the routes associated with mapped domains
}
else {
    require app_path().'/Http/Routes/market.php'; // all the routes associated with the platform
}

The facade can, of course, also be used in a controller or anywhere else, however, you can also simply inject the DomainContext object into your controller method, e.g.,

public function show($id, DomainContext $context)
{
    $short = $context->getDomainIdentifier();
    $tc = Company::where('short_name', '=', $short)->first();
    ...

Finally, within views, the DomainContext is always available as $domainContext (or the variable name you set in the configuration file). For example, you might have part of a menu depend on whether domain is mapped or not:

<li class="dropdown">
    @if ($domainContext->isMapped())
        <a href="/products" class="dropdown-toggle" >All Products</a>
    @else
        <a href="#" class="dropdown-toggle" data-toggle="dropdown">Market</a>
        <ul class="dropdown-menu">
            <li><a href="/market/products">Products</a></li>
            <li><a href="/market/companies">Companies</a></li>
        </ul>
    @endif
</li>

Problems and Plans

This module is being used for a couple products in active development and will probably evolve. If you find bugs or have requests for features, create an issue here, find me on Twitter (@ejaxon) or submit a pull request.

democracyapps/domain-context 适用场景与选型建议

democracyapps/domain-context 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 118 次下载、GitHub Stars 达 1, 最近一次更新时间为 2015 年 03 月 18 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 democracyapps/domain-context 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 118
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 25
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-03-18