【问题标题】:How to use Laravel Package LandLord如何使用 Laravel 包地主
【发布时间】:2018-03-22 23:40:58
【问题描述】:

我对 Laravel 还是很陌生,并且已经完成了一些基本的 laracast。现在我开始了我的第一个 laravel 项目,但我被困在如何使用我的第一个包“Landlord”上。基本上我需要在我的应用程序中设置多租户。我有一个公司表和一个用户表,用户表有一个 company_id 列。当公司注册时,它会成功创建公司并将 company_id 附加到用户。

我认为 Landlord 是实现多租户应用程序的最佳方式,因此我按照安装说明进行操作,现在我将其包含在我的应用程序中。

但是,USAGE 部分的第一行说: 重要提示:房东是无国籍的。这意味着当您调用 addTenant() 时,它只会作用于当前请求。

确保您添加租户的方式是 发生在每个请求上,并且在您需要模型范围之前,例如在 中间件或作为 OAuth 等无状态身份验证方法的一部分。

看起来我需要附加一个Landlord::addTenant('tenant_id', 1); 门面。

这可能是一个我忽略的非常简单的答案,但是使用addTenant 的最佳位置在哪里,我是否必须在每个控制器或模型中重新声明它?我应该在用户登录时附加它,在我的路由中使用它还是用作中间件?如果它是一个中间件,那么为了从当前用户中提取 company_id 并将其与addTenant 一起使用,以下是正确的吗?

中间件:

public function handle($request, Closure $next){
    $tenantId = Auth::user()->tenant_id;

    Landlord::addTenant('tenant_id', $tenantId);

    return $next($request);
}

更新

这是我的中间件 (MultiTenant.php)

<?php

namespace App\Http\Middleware;

use Closure;
use App\User;
use Illuminate\Support\Facades\Auth;

class MultiTenant
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */

    public function handle($request, Closure $next)
    {        
        if (Auth::check()) {
            $tenantId = Auth::user()->company_id;

            Landlord::addTenant('company_id', $tenantId); // Different column name, but same concept
        }

        return $next($request);
    }
}

我的路线/web.php

<?php

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| This file is where you may define all of the routes that are handled
| by your application. Just tell Laravel the URIs it should respond
| to using a Closure or controller method. Build something great!
|
*/

Route::get('/', function () {
    return view('welcome');
});

Auth::routes();
Route::group(['middleware' => ['multitenant']], function () {
    Route::get('/home', 'HomeController@index');

    //Clients
    Route::resource('clients', 'ClientController');
});

我的 Client.php 模型:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use HipsterJazzbo\Landlord\BelongsToTenants;

class Client extends Model
{
    use BelongsToTenants;
    //
    protected $fillable = [
        'organization',
    ];

}

https://github.com/HipsterJazzbo/Landlord#user-content-usage

【问题讨论】:

  • 如果所有包含租户数据的表都具有tenant_id 键,那么它们都将使用全局附加全局范围WHEREtenant_id = ID 进行查询(当然如果它们具有BelongsToTenants 特征) .是的,中间件是添加租户的最佳场所,恕我直言

标签: php laravel laravel-5.2


【解决方案1】:

虽然只有一种选择,但我也选择了middleware 路线。我认为这是实现它的一种简单方法。

我将middleware 添加到我的routes/web.php 文件中:

Route::group(['middleware' => ['landlord']], function () {
    // Your routes
});

而我的landlord middleware 看起来像这样:

public function handle($request, Closure $next)
{        
    if (Auth::check()) {
        $tenantId = Auth::user()->company_id;

        Landlord::addTenant('company_id', $tenantId); // Different column name, but same concept
    }

    return $next($request);
}

然后我只需将trait 添加到我想要作用域的模型中:

use HipsterJazzbo\Landlord\BelongsToTenant;

class User extends Authenticatable
{
    use BelongsToTenant;
}

更新

另外,请确保在您的 config/app.php 文件中,您已将 landlord 添加到 providers 数组中:

'providers' => [
    // ...
    HipsterJazzbo\Landlord\LandlordServiceProvider::class
    // ...
],

aliases 数组:

'aliases' => [

    // ...
    'Landlord'   => HipsterJazzbo\Landlord\Facades\Landlord::class,
    // ...
],

然后最后composer dump-autoload完成后刷新自动加载。

【讨论】:

  • 我的方向是正确的。我已经完成了您在此处建议的所有操作,但我从我的模型中收到了错误 Trait 'HipsterJazzbo\Landlord\BelongsToTenant' not found。有什么想法吗?
  • @Derek 是的,你是!你更新了你的app.php 配置文件吗?
  • 是的,我按照 github 页面上的设置说明进行操作。这是我的 bootstrap/app.php 文件的屏幕截图:imgur.com/a/gdtB6
  • @Derek 抱歉,我说的是你的config/app.php 文件。让我更新我的答案以确保我们在同一页面上。
  • @Derek 这个错误是说它找不到你的Landlord 类。如果您如上所示将其添加到 aliases 数组中,请确保将其导入到您的 MultiTenant.php 文件的顶部:use Landlord;
猜你喜欢
  • 1970-01-01
  • 2020-11-26
  • 2019-12-26
  • 1970-01-01
  • 2016-12-08
  • 2015-08-12
  • 2016-12-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多