kofoworola/laravel-subdomain 问题修复 & 功能扩展

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

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

kofoworola/laravel-subdomain

Composer 安装命令:

composer require kofoworola/laravel-subdomain

包简介

README 文档

README

Easily setup dynamic subdomains registered to models on your application e.g company1.myapp.com

Installation

You can install this package via composer by

composer require kofoworola/laravel-subdomain

*In Laravel 5.5 and below add this to your providers and aliases array

'providers'=>[
    //...
    kofoworola\Subdomains\ServiceProvider::class
]
'aliases' => [
    //..
    'Subdomains': kofoworola\Subdomains\Facade\Subdomains::class
] 

Then run php artisan vendor:publish to publish the configuration file

Config

The config/subdomains.php file:

<?php
return [
    /*
     * The name of the parameter to be used for subdomains
     */
    'subdomain' => 'app',

    /*
     * The model that owns subdomains
     */
    'model' => '\App\Company',

    /*
     * The column in the user
     */
    'column' => 'slug',

    'middleware' => [

        /*
         * The user model to perform authentication checking on
         */
        'user_model' => '\App\User',

        /*
         * The function/relationship that defines the link between the user model and the subdomain owner 
         * Can return Collection/Array of Models or single model
         */
        'function' => 'companies',
    ]
];

Once you have published the package, you can then set the configuration to your application's setup

Usage

After setting up your routes to catch subdomains e.g

Route::group(['domain' => '{app}.subdomains.app','middleware' => ['auth']],function (){
    Route::get('/','SubController@index')->name('index');
    Route::get('/{param}',SubController@param)->name('param')
    //Rest of routes
});

Make sure all controller assigned to subdomains extend the \kofoworola\Subdomains\Controller\SubdomainController class:

use kofoworola\Subdomains\Controller\SubdomainController;

class SubController extends SubdomainController
{
    public function index(){
        return 'hello';
    }

    /*
     * No need to get the subdomain value via parameter for every method redundantly
     * You can get it through the subdomain facade when needed
     */ 
    public function param($param){
        return $param;
    }
}

The parent controller automatically removes the subdomain parameter from list of paremeters so you don't have to add it to you parameter list everytime you want to get another parameter

Facade

Using the kofoworola\Subdomains\Facade\Subdomains facade you can access helper functions:

Getting the name of the parameter

Use Subdomains::name() to get the name of the subdomain parameter

Getting the value of the subdomain

Use Subdomains::value() to get the value of the subdomain parameter

Getting the owner of the subdomain

Use Subdomains::owner() to get the model instance that owns the current subdomain

Checking if the user has access to the subdomain

Subdomains::ownsModel($user = null) returns true or false depending on whether the user has access to the subdomain

If no user is passed the current logged in user is used

Getting a subdomain route

//If model is passed the value of its subdomain will be used to generate route
//If not the current owner will be used

Subdomains::route('route.name',$params = [],$model = null);

Can be used to generate a subdomain link

Middleware

You can also add the \kofoworola\Subdomains\Middleware\HasSubdomain middleware to your routes. Firstly register it in your Kernel:

protected $routeMiddleware = [
        //Rest of middlewares
        'subdomain' => \kofoworola\Subdomains\Middleware\HasSubdomain::class,
    ];

Then add it to your routes:

Route::group(['domain' => '{app}.subdomains.app','middleware' => ['auth','subdomains']],function (){
    //Rest of routes
});

The middleware will take care of verifying if:

  • The subdomain exists
  • The current user has access to the subdomain

kofoworola/laravel-subdomain 适用场景与选型建议

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

  • Stars: 11
  • Watchers: 2
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: GPL-3.0-only
  • 更新时间: 2018-10-22