2.x update some content

This commit is contained in:
Samuel Štancl 2019-09-22 11:16:49 +02:00
parent 667467ddc4
commit 60d320ad6a
11 changed files with 144 additions and 86 deletions

View file

@ -65,7 +65,9 @@ return [
'url' => 'usage', 'url' => 'usage',
'children' => [ 'children' => [
'Creating Tenants' => 'creating-tenants', 'Creating Tenants' => 'creating-tenants',
'Tenant Migrations'=> 'tenant-migrations',
'Tenant Routes' => 'tenant-routes', 'Tenant Routes' => 'tenant-routes',
'Tenant Objects' => 'tenant', // todo rename route?
'Tenant Storage' => 'tenant-storage', 'Tenant Storage' => 'tenant-storage',
'Tenant Manager' => 'tenant-manager', 'Tenant Manager' => 'tenant-manager',
'Console Commands' => 'console-commands', 'Console Commands' => 'console-commands',

View file

@ -81,7 +81,7 @@
@yield('body') @yield('body')
</main> </main>
<script src="{{ mix('js/main.js', 'assets/build') }}"></script> <script src="{{ $page->baseUrl . mix('js/main.js', 'assets/build') }}"></script>
@stack('scripts') @stack('scripts')

View file

@ -81,7 +81,7 @@
@yield('body') @yield('body')
</main> </main>
<script src="{{ mix('js/main.js', 'assets/build') }}"></script> <script src="{{ $page->baseUrl . mix('js/main.js', 'assets/build') }}"></script>
@stack('scripts') @stack('scripts')

View file

@ -96,25 +96,25 @@ The aliases are used by the [event system]({{ $page->link('event-system') }})
Features are similar to bootstrappers, but they are executed regardless of whether tenancy has been initialized or not. Their purpose is to provide additional functionality that is not necessary for the package to work. Things like easy redirects to tenant domains, tags in Telescope, etc. Features are similar to bootstrappers, but they are executed regardless of whether tenancy has been initialized or not. Their purpose is to provide additional functionality that is not necessary for the package to work. Things like easy redirects to tenant domains, tags in Telescope, etc.
### `migrate_after_creation` ### `migrate_after_creation` {#migrate-after-creation}
Run migrations after creating a tenant. Run migrations after creating a tenant.
- Default: `false` - Default: `false`
### `delete_database_after_tenant_deletion` ### `delete_database_after_tenant_deletion` {#delete-database-after-tenant-deletion}
Delete the tenant's database after deleting the tenant. Delete the tenant's database after deleting the tenant.
- Default: `false` - Default: `false`
### `queue_database_creation` ### `queue_database_creation` {#queue-database-creation}
- Default: `false` - Default: `false`
### `queue_database_deletion` ### `queue_database_deletion` {#queue-database-deletion}
- Default: `false` - Default: `false`
### `unique_id_generator` ### `unique_id_generator` {#unique-id-generator}
The class used to generate a random tenant ID (when no ID is supplied during the tenant creation process). The class used to generate a random tenant ID (when no ID is supplied during the tenant creation process).

View file

@ -1,6 +1,6 @@
--- ---
title: Creating Tenants title: Creating Tenants
description: Creating tenants.. description: Creating tenants
extends: _layouts.documentation_v2 extends: _layouts.documentation_v2
section: content section: content
--- ---
@ -12,14 +12,19 @@ section: content
To create a tenant, you can use To create a tenant, you can use
```php ```php
Tenant::new()->withDomains(['tenant1.yourapp.com'])->withData(['plan' => 'free'])->save(); use Stancl\Tenancy\Tenant;
Tenant::new()
->withDomains(['tenant1.yourapp.com', 'tenant1.com'])
->withData(['plan' => 'free'])
->save();
``` ```
> Tip: All domains under `.localhost` are routed to 127.0.0.1 on most operating systems. This is useful for development. > Tip: All domains under `.localhost` are routed to 127.0.0.1 on most operating systems. This is useful for development.
The `withDomains()` and `withData()` methods are optional. The `withDomains()` and `withData()` methods are optional.
You can also create a tenant using the `Tenant::create` method: You can also create a tenant using a single method: `Tenant::create`:
```php ```php
$domains = ['tenant1.myapp.com', 'tenant1.com']; $domains = ['tenant1.myapp.com', 'tenant1.com'];
@ -28,4 +33,6 @@ Tenant::create($domains, [
]); ]);
``` ```
> Note: Creating a tenant doesn't run [migrations](https://stancl-tenancy.netlify.com/docs/console-commands/#migrate) automatically. You have to do that yourself. <!-- TODO auto migrate after creation --> `Tenant::create()` works with both `Stancl\Tenancy\Tenant` and the facade, `\Tenant`.
> Note: By default, creating a tenant doesn't run [migrations]({{ $page->link('tenant-migrations' )}}) automatically. You may change this behavior using the `migrate_after_creation` [configuration]({{ $page->link('configuration#migrate-after-creation') }}).

View file

@ -1,43 +1,29 @@
--- ---
title: Tenant Manager title: Tenant Manager
description: Tenant Manager. description: Tenant Manager
extends: _layouts.documentation_v2 extends: _layouts.documentation_v2
section: content section: content
--- ---
# Tenant Manager {#tenant-manager} # Tenant Manager {#tenant-manager}
This page documents a couple of other `TenantManager` methods you may find useful. This page documents a couple of `TenantManager` methods you may find useful.
To call methods on `TenantManager`, you may use the `tenancy()` helper or the `Tenancy` facade.
### Finding tenant using id ### Finding tenant using id
`find()` is an alias for `getTenantById()`. You may use the second argument to specify the key(s) as a string/array.
```php ```php
>>> tenant()->find('dbe0b330-1a6e-11e9-b4c3-354da4b4f339'); >>> \Tenancy::find('b07aa3b0-dc68-11e9-9352-9159b2055c42')
=> [ => Stancl\Tenancy\Tenant {#3099
"id" => "dbe0b330-1a6e-11e9-b4c3-354da4b4f339", +data: [
"domain" => "localhost", "id" => "b07aa3b0-dc68-11e9-9352-9159b2055c42",
"foo" => "bar", "plan" => "free",
] ],
>>> tenant()->find('dbe0b330-1a6e-11e9-b4c3-354da4b4f339', 'foo'); +domains: [
=> [ "foo.localhost",
"foo" => "bar", ],
] }
>>> tenant()->getTenantById('dbe0b330-1a6e-11e9-b4c3-354da4b4f339', ['foo', 'domain']);
=> [
"foo" => "bar",
"domain" => "localhost",
]
```
### Getting tenant ID by domain
```php
>>> tenant()->getTenantIdByDomain('localhost');
=> "b3ce3f90-1a88-11e9-a6b0-038c6337ae50"
>>> tenant()->getIdByDomain('localhost');
=> "b3ce3f90-1a88-11e9-a6b0-038c6337ae50"
``` ```
### Finding tenant by domain ### Finding tenant by domain
@ -45,25 +31,29 @@ This page documents a couple of other `TenantManager` methods you may find usefu
You may use the second argument to specify the key(s) as a string/array. You may use the second argument to specify the key(s) as a string/array.
```php ```php
>>> tenant()->findByDomain('localhost'); >>> tenancy()->findByDomain('bar.localhost')
=> [ => Stancl\Tenancy\Tenant {#3091
"id" => "b3ce3f90-1a88-11e9-a6b0-038c6337ae50", +data: [
"domain" => "localhost", "id" => "b38b2bd0-dc68-11e9-adfc-ede94ab3b264",
] ],
+domains: [
"bar.localhost",
],
}
``` ```
### Accessing the array ### Getting the current tenant
You can access the public array tenant of TenantManager like this: One more way to get the current [tenant]({{ $page->link('tenant') }}) is to call `getTenant()` on `TenantManager`:
```php ```php
tenancy()->tenant tenancy()->getTenant()
``` ```
which is an array. If you want to get the value of a specific key from the array, you can use one of the helpers the key on the tenant array as an argument. If you want to get the value of a specific key from the array, you can an argument with the key.
```php ```php
tenant('id'); // Does the same thing as tenancy()->getTenant('id') tenancy()->getTenant('id'); // Does the same thing as tenant('id')
``` ```
### Getting all tenants ### Getting all tenants
@ -71,24 +61,36 @@ tenant('id'); // Does the same thing as tenancy()->getTenant('id')
This method returns a collection of arrays. This method returns a collection of arrays.
```php ```php
>>> tenant()->all(); >>> tenancy()->all()
=> Illuminate\Support\Collection {#2980 => Illuminate\Support\Collection {#3080
all: [ all: [
[ Stancl\Tenancy\Tenant {#3076
"id" => "32e20780-1a88-11e9-a051-4b6489a7edac", +data: [
"domain" => "localhost", "id" => "b07aa3b0-dc68-11e9-9352-9159b2055c42",
], ],
[ +domains: [
"id" => "49670df0-1a87-11e9-b7ba-cf5353777957", "foo.localhost",
"domain" => "dev.localhost",
], ],
},
Stancl\Tenancy\Tenant {#3075
+data: [
"id" => "b38b2bd0-dc68-11e9-adfc-ede94ab3b264",
],
+domains: [
"bar.localhost",
],
},
], ],
} }
>>> tenant()->all()->pluck('domain'); >>> tenancy()->all()->pluck('domains')
=> Illuminate\Support\Collection {#2983 => Illuminate\Support\Collection {#3108
all: [ all: [
"localhost", [
"dev.localhost", "foo.localhost",
],
[
"bar.localhost",
],
], ],
} }
``` ```
@ -96,17 +98,25 @@ This method returns a collection of arrays.
### Deleting a tenant ### Deleting a tenant
```php ```php
>>> tenant()->delete('dbe0b330-1a6e-11e9-b4c3-354da4b4f339'); >>> $tenant = tenancy()->findByDomain('foo.localhost');
=> true => Stancl\Tenancy\Tenant {#3119
>>> tenant()->delete(tenant()->getTenantIdByDomain('dev.localhost')); +data: [
=> true "id" => "b07aa3b0-dc68-11e9-9352-9159b2055c42",
>>> tenant()->delete(tenant()->findByDomain('localhost')['uuid']); "plan" => "free",
],
+domains: [
"foo.localhost",
],
}
>>> $tenant->delete();
=> true => true
``` ```
This doesn't delete the tenant's database. If you want to delete it, save the database name prior to deleting the tenant. You can get the database name using `getDatabaseName()` This doesn't delete the tenant's database. If you want to delete it, save the database name prior to deleting the tenant. You can get the database name using `getDatabaseName()`
```php ```php
>>> tenant()->getDatabaseName(tenant()->findByDomain('laravel.localhost')) >>> $tenant->getDatabaseName()
=> "tenant67412a60-1c01-11e9-a9e9-f799baa56fd9" => "tenant67412a60-1c01-11e9-a9e9-f799baa56fd9"
``` ```
If you want tenant databases to be deleted automatically, you may use the [`delete_database_after_tenant_deletion` configuration]({{ $page->link('configuration#delete-database-after-tenant-deletion') }})

View file

@ -0,0 +1,30 @@
---
title: Tenant Migrations
description: Tenant Migrations
extends: _layouts.documentation_v2
section: content
---
# Tenant Migrations {#tenant-migrations}
You can run tenant migrations using the `php artisan tenants:migrate` command.
You may specify the tenant(s) using the `--tenants` option.
```
php artisan tenants:migrate --tenants=8075a580-1cb8-11e9-8822-49c5d8f8ff23
```
> Note: Tenant migrations must be located in `database/migrations/tenant`.
You can run migrations from outside the command line as well. To run migrations for a tenant in your code, use `Artisan::call()`:
```php
$tenant = \Tenant::create('tenant1.localhost');
\Artisan::call('tenants:migrate', [
'--tenants' => [$tenant->id];
]);
```
You may also [configure]({{ $page->link('configuration#migrate-after-creation') }}) the package to run migrations automatically, after creating tenants.

View file

@ -11,7 +11,7 @@ Routes within `routes/tenant.php` will have the `web` middleware group and the `
Just like `routes/web.php`, these routes use the `App\Http\Controllers` namespace. Just like `routes/web.php`, these routes use the `App\Http\Controllers` namespace.
> If a tenant cannot be identified, anexception will be thrown. If you want to change this behavior (to a redirect, for example) read the [Middleware Configuration]({{ $page->link('middleware-configuration') }}) page. > If a tenant cannot be identified, an exception will be thrown. If you want to change this behavior (to a redirect, for example) read the [Middleware Configuration]({{ $page->link('middleware-configuration') }}) page.
## Exempt routes {#exempt-routes} ## Exempt routes {#exempt-routes}

View file

@ -7,30 +7,29 @@ section: content
# Tenant Storage {#tenant-storage} # Tenant Storage {#tenant-storage}
Tenant storage is where tenants' ids and domains are stored. You can store things like the tenant's plan, subscription information, and tenant-specific application configuration in tenant storage. You can use these functions: Tenant storage is where tenants' ids and domains are stored. You can store things like the tenant's plan, subscription information, and tenant-specific application configuration in tenant storage. You may use these functions on `Tenant` objects:
```php ```php
get (string|array $key, string $id = null) // $id defaults to the current tenant's id get (string|array $key)
put (string|array $key, mixed $value = null, string $id = null) // if $key is array, make sure $value is null put (string|array $key, mixed $value = null) // if $key is array, make sure $value is null
``` ```
To put something into the tenant storage, you can use `put()` or `set()`. To put something into the tenant storage, you can use `put()` or `set()`.
```php ```php
tenancy()->put($key, $value); $tenant->put($key, $value);
tenancy()->set($key, $value); // alias for put() $tenant->set($key, $value); // alias for put()
tenancy()->put($key, $value, $id); $tenant->put(['key1' => 'value1', 'key2' => 'value2']);
tenancy()->put(['key1' => 'value1', 'key2' => 'value2']);
tenancy()->put(['key1' => 'value1', 'key2' => 'value2'], null, $id);
``` ```
To get something from the storage, you can use `get()`: To get something from the storage, you can use `get()`:
```php ```php
tenancy()->get($key); tenant()->get($key);
tenancy()->get($key, $id); tenant()->get(['key1', 'key2']);
tenancy()->get(['key1', 'key2']);
``` ```
> Note: `tenancy()->get(['key1', 'key2'])` returns an array with values only > In this example, we're calling the methods on the current tenant &mdash; `tenant()`.
> Note: `get(['key1', 'key2'])` returns an associative array.
Note that $key has to be a string or an array with string keys. The value(s) can be of any data type. Example with arrays: Note that $key has to be a string or an array with string keys. The value(s) can be of any data type. Example with arrays:

10
docs/source/v2/tenant.md Normal file
View file

@ -0,0 +1,10 @@
---
title: Tenant Objects
description: Tenant Objects
extends: _layouts.documentation_v2
section: content
---
# Tenant Objects {#tenant-objects}
todo

View file

@ -9,8 +9,8 @@ section: content
This chapter describes usage of the package. That includes creating tenants, deleting tenants, storing data in the tenant storage. This chapter describes usage of the package. That includes creating tenants, deleting tenants, storing data in the tenant storage.
This package comes with two helpers - `tenancy()` and `tenant()`. `tenancy()` returns an instance of `TenantManager` and should be primarily used only for `tenancy()->all()`, but for legacy reasons it can be used to create tenants. The package comes with two helpers - `tenancy()` and `tenant()`.
- `tenancy()` returns an instance of [`TenantManager`]({{ $page->link('tenant-manager') }})
You can pass an argument to the helper function to get a value out of the tenant storage. `tenant('plan')` is identical to [`tenant()->get('plan')`]({{ $page->link('tenant-storage') }}). - `tenant()` returns an instance of the current tenant, or null if no tenant hs been identified yet. You can pass an argument to this helper, to get a value from the tenant storage: `tenant('plan')` is identical to [`tenant()->get('plan')`]({{ $page->link('tenant-storage') }}).
The package also comes with two facades. `Tenancy` -- for `TenantManager` -- and `Tenant` -- for the current `Tenant`, or null if no tenant has been identified yet. The package also comes with two facades. `Tenancy` -- for `TenantManager` -- and `Tenant` -- for the current `Tenant`, or null if no tenant has been identified yet.