Generating custom urls | laravel-medialibrary | Spatie

 SPATIE

  Laravel Media Library
========================

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Laravel-medialibrary](https://spatie.be/docs/laravel-medialibrary/v3)  Advanced-usage  Generating custom urls

 Version   v11   v10   v9   v8   v7   v6   v5   v4   v3

 Other versions for crawler [v11](https://spatie.be/docs/laravel-medialibrary/v11) [v10](https://spatie.be/docs/laravel-medialibrary/v10) [v9](https://spatie.be/docs/laravel-medialibrary/v9) [v8](https://spatie.be/docs/laravel-medialibrary/v8) [v7](https://spatie.be/docs/laravel-medialibrary/v7) [v6](https://spatie.be/docs/laravel-medialibrary/v6) [v5](https://spatie.be/docs/laravel-medialibrary/v5) [v4](https://spatie.be/docs/laravel-medialibrary/v4) [v3](https://spatie.be/docs/laravel-medialibrary/v3)

- [ Introduction ](https://spatie.be/docs/laravel-medialibrary/v3/introduction)
- [ Postcardware ](https://spatie.be/docs/laravel-medialibrary/v3/postcardware)
- [ Requirements ](https://spatie.be/docs/laravel-medialibrary/v3/requirements)
- [ Installation &amp; setup ](https://spatie.be/docs/laravel-medialibrary/v3/installation-setup)
- [ Questions and issues ](https://spatie.be/docs/laravel-medialibrary/v3/questions-issues)
- [ Changelog ](https://spatie.be/docs/laravel-medialibrary/v3/changelog)

Basic usage
-----------

- [ Preparing your model ](https://spatie.be/docs/laravel-medialibrary/v3/basic-usage/preparing-your-model)
- [ Associating files ](https://spatie.be/docs/laravel-medialibrary/v3/basic-usage/associating-files)
- [ Retrieving media ](https://spatie.be/docs/laravel-medialibrary/v3/basic-usage/retrieving-media)
- [ Working with collections ](https://spatie.be/docs/laravel-medialibrary/v3/basic-usage/working-with-collections)

Converting images
-----------------

- [ Defining conversions ](https://spatie.be/docs/laravel-medialibrary/v3/converting-images/defining-conversions)
- [ Retrieving converted images ](https://spatie.be/docs/laravel-medialibrary/v3/converting-images/retrieving-converted-images)
- [ Regenerating images ](https://spatie.be/docs/laravel-medialibrary/v3/converting-images/regenerating-images)

Advanced usage
--------------

- [ Working with multiple filesystems ](https://spatie.be/docs/laravel-medialibrary/v3/advanced-usage/working-with-multiple-filesystems)
- [ Storing media specific manipulations ](https://spatie.be/docs/laravel-medialibrary/v3/advanced-usage/storing-media-specific-manipulations)
- [ Using your own model ](https://spatie.be/docs/laravel-medialibrary/v3/advanced-usage/using-your-own-model)
- [ Using a custom directory structure ](https://spatie.be/docs/laravel-medialibrary/v3/advanced-usage/using-a-custom-directory-structure)
- [ Consuming events ](https://spatie.be/docs/laravel-medialibrary/v3/advanced-usage/consuming-events)
- [ Generating custom urls ](https://spatie.be/docs/laravel-medialibrary/v3/advanced-usage/generating-custom-urls)
- [ Adding custom properties ](https://spatie.be/docs/laravel-medialibrary/v3/advanced-usage/adding-custom-properties)

API
---

- [ Adding files ](https://spatie.be/docs/laravel-medialibrary/v3/api/adding-files)
- [ Defining conversions ](https://spatie.be/docs/laravel-medialibrary/v3/api/defining-conversions)

      You are viewing the documentation for **an older version** of this package. You can check the version you are using with the following command:

 `                                    composer show spatie/laravel-medialibrary                                                                                                                                                                                                                                    `

Generating custom urls
======================

When `getUrl` is called, the task of generating that URL is passed to an implementation of `Spatie\MediaLibraryUrlGenerator`.

The package contains a `LocalUrlGenerator` that can generate URLs for a media library that is stored inside the public path. An `S3UrlGenerator` is also included for when you're using S3 to store your files.

If you are storing your media files in a private directory or are using a different filesystem, you can write your own `UrlGenerator`. Your generator must adhere to the `Spatie\MediaLibraryUrlGenerator` interface. If you'd extend `Spatie\MediaLibraryUrlGenerator\BaseGenerator` you only need to implement one method: `getUrl`, which should return the URL. You can call `getPathRelativeToRoot` to get the relative path to the root of your disk.

The code of the included `S3UrlGenerator` should help make things more clear:

```
 namespace Spatie\MediaLibrary\UrlGenerator;

 use Spatie\MediaLibrary\Exceptions\UrlCouldNotBeDeterminedException;

 class S3UrlGenerator extends BaseUrlGenerator implements UrlGenerator
 {
     /**
      * Get the URL for the profile of a media item.
      *
      * @return string
      *
      * @throws UrlCouldNotBeDeterminedException
      */
     public function getUrl()
     {
         return config('laravel-medialibrary.s3.domain').'/'.$this->getPathRelativeToRoot();
     }
 }
```
