Add spatie/laravel-medialibrary integration guide (#194)

This commit is contained in:
lukinovec 2022-09-01 18:05:38 +02:00 committed by GitHub
parent 4c7eeb9a99
commit aca5b15aba
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -42,3 +42,29 @@ Event::listen(TenancyEnded::class, function (TenancyEnded $event) {
```
The reason for this is that spatie/laravel-permission caches permissions & roles to save DB queries, which means that we need to separate the permission cache by tenant.
## **laravel-medialibrary** {#laravel-medialibrary}
To generate the correct media URLs for tenants, create a custom URL generator class extending `Spatie\MediaLibrary\Support\UrlGenerator\DefaultUrlGenerator` and override the `getUrl()` method:
```php
use Spatie\MediaLibrary\Support\UrlGenerator\DefaultUrlGenerator;
class TenantAwareUrlGenerator extends DefaultUrlGenerator
{
public function getUrl(): string
{
$url = asset($this->getPathRelativeToRoot());
$url = $this->versionUrl($url);
return $url;
}
}
```
Then, change the `url_generator` in the media-library config to the custom one (make sure to import it):
```php
'url_generator' => TenantAwareUrlGenerator::class,
```