bvharen/laravel-subdomain
Composer 安装命令:
composer require bvharen/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
bvharen/laravel-subdomain 适用场景与选型建议
bvharen/laravel-subdomain 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 14 次下载、GitHub Stars 达 0, 最近一次更新时间为 2021 年 01 月 20 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 bvharen/laravel-subdomain 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 bvharen/laravel-subdomain 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 14
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: GPL-3.0-only
- 更新时间: 2021-01-20