简体   繁体   English

如何使用Laravel Package LandLord

[英]How to use Laravel Package LandLord

Im still fairly new to Laravel and have worked through some of the fundamental laracasts. 我对Laravel还是很陌生,并且已经完成了一些基本的laracast。 Now I'm starting my first laravel project but I am stuck on how to use my first package "Landlord". 现在,我开始我的第一个laravel项目,但是我仍然坚持如何使用我的第一个软件包“ Landlord”。 Basically I need a setup for Multi-Tenants in my application. 基本上,我需要在应用程序中设置多租户。 I have a company table and a user table, the user table has a company_id column. 我有一个公司表和一个用户表,该用户表有一个company_id列。 When a company registers it successfully creates the company and attaches the company_id to the user. 公司注册后,它会成功创建公司,并将company_id附加到用户。

I assume Landlord is the best way to implement a multi-tenant application so I worked through the installation instructions and now I have it included in my app. 我认为Landlord是实现多租户应用程序的最佳方法,因此我按照安装说明进行了操作,现在将其包含在我的应用程序中。

However the first line in the USAGE section says: IMPORTANT NOTE: Landlord is stateless. 但是,“用法”部分的第一行说:重要说明:房东是无国籍的。 This means that when you call addTenant(), it will only scope the current request. 这意味着,当您调用addTenant()时,它将仅作用域当前请求。

Make sure that you are adding your tenants in such a way that it happens on every request, and before you need Models scoped, like in a middleware or as part of a stateless authentication method like OAuth. 确保以某种方式添加租户,使其在每次请求时都发生,并且在需要对模型进行范围划分之前,例如在中间件中或作为无状态身份验证方法(如OAuth)的一部分。

And it looks like I need to attach a the Landlord::addTenant('tenant_id', 1); 看来我需要附加一个Landlord::addTenant('tenant_id', 1); facade. 正面。

This may be a pretty simple answer I am overlooking but where is the best place to to use addTenant and do I have to redeclare it with every controller or model? 这可能是我忽略的一个非常简单的答案,但是在哪里可以使用addTenant呢?在每个控制器或模型中都必须重新声明它吗? Should I attach it when the user signs in, use it in my routes or use as a middleware? 用户登录时应该将其附加,在我的路线中使用它还是用作中间件? If it is a middleware is the following correct in order to pull the company_id from the current user and use it with addTenant ? 如果是中间件,为了从当前用户中提取company_id并将其与addTenant一起使用,以下是否正确?

Middleware: 中间件:

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

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

    return $next($request);
}

UPDATE 更新

Here is my middleware (MultiTenant.php) 这是我的中间件(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);
    }
}

My routes/web.php 我的路线/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');
});

My Client.php Model: 我的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 https://github.com/HipsterJazzbo/Landlord#user-content-usage

While just one option, I also went the middleware route. 虽然只是一种选择,但我也选择了middleware I saw it as an easy way to implement it. 我认为这是实现它的一种简便方法。

I added the middleware to my routes/web.php file: 我将middleware添加到了routes/web.php文件中:

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

And my landlord middleware looks like this: 我的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);
}

Then I just add the trait to the models that I want scoped: 然后,我只是将trait添加到我要确定范围的模型中:

use HipsterJazzbo\Landlord\BelongsToTenant;

class User extends Authenticatable
{
    use BelongsToTenant;
}

Update 更新资料

Also, make sure in your config/app.php file you have added landlord to the providers array: 另外,请确保在config/app.php文件中,已将landlord添加到providers数组中:

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

And to the aliases array: aliases数组:

'aliases' => [

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

Then finally composer dump-autoload when completed to refresh the autoloading. 然后在完成后最终完成composer dump-autoload刷新自动加载。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM