From 02f6f071fa997aa96268f5344a61177aaf8e329b Mon Sep 17 00:00:00 2001 From: lukinovec Date: Wed, 31 Aug 2022 12:38:23 +0200 Subject: [PATCH] Add spatie/laravel-medialibrary integration guide --- source/docs/v3/integrations/spatie.blade.md | 26 +++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/source/docs/v3/integrations/spatie.blade.md b/source/docs/v3/integrations/spatie.blade.md index fc68946..ec16cf3 100644 --- a/source/docs/v3/integrations/spatie.blade.md +++ b/source/docs/v3/integrations/spatie.blade.md @@ -38,3 +38,29 @@ Event::listen(TenancyBootstrapped::class, function (TenancyBootstrapped $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, +```