spatie / laravel-medialibrary

Associate files with Eloquent models
https://spatie.be/docs/laravel-medialibrary
MIT License
5.76k stars 1.07k forks source link

Class Spatie\MediaLibrary\UrlGenerator\UrlGenerator doesn't exist #434

Closed loranger closed 7 years ago

loranger commented 7 years ago

Hello,

I try to use my existing Laravel Models (already using media-library) inside a Lumen project. I setup laravel-medialibrary for lumen as written in the documentation. Everything seems to work perfectly, but as soon as I try to get a $media->getUrl(), I get this exception :

InvalidUrlGenerator in InvalidUrlGenerator.php line 11: Class Spatie\MediaLibrary\UrlGenerator\UrlGenerator doesn't exist

1. in InvalidUrlGenerator.php line 11
2. at InvalidUrlGenerator::doesntExist('Spatie\MediaLibrary\UrlGenerator\UrlGenerator') in UrlGeneratorFactory.php line 29
3. at UrlGeneratorFactory::guardAgainstInvalidUrlGenerator('Spatie\MediaLibrary\UrlGenerator\UrlGenerator') in UrlGeneratorFactory.php line 16
4. at UrlGeneratorFactory::createForMedia(object(Media)) in Media.php line 59
5. at Media->getUrl() in Venue.php line 284
6. at Venue->getPicturesUrlAttribute(null) in Model.php line 2726

Can you help me to figure out what is my mistake ?

freekmurze commented 7 years ago

If you haven't specified an custom url generator in the config file, the package will try to find an url generator for the driver of the disk the media is stored upon. That happens here: https://github.com/spatie/laravel-medialibrary/blob/19b4d5c36b567926e14fdef591f8d7388933f035/src/UrlGenerator/UrlGeneratorFactory.php#L14

Which disk driver are you using? What the value of disk in the media table?

loranger commented 7 years ago

Thanks for your help

I have a custom url generator (the same I use with Laravel: I'm trying to my project api from Laravel to Lumen, without loosing any feature) and a custom path generator.

Here is my laravel-medialibrary.php configuration file :

<?php

return [

    /*
     * The filesystems on which to store added files and derived images by default. Choose
     * one or more of the filesystems you configured in app/config/filesystems.php
     */
    'defaultFilesystem'           => 'media',

    /*
     * The maximum file size of an item in bytes. Adding a file
     * that is larger will result in an exception.
     */
    'max_file_size'               => 1024 * 1024 * 20,

    /*
     * This queue will used to generate derived images.
     * Leave empty to use the default queue.
     */
    'queue_name'                  => '',

    /*
     * The class name of the media model to be used.
     */
    'media_model'                 => Spatie\MediaLibrary\Media::class,

    /*
     * When urls to files get generated this class will be called. Leave empty
     * if your files are stored locally above the site root or on s3.
     */
    'custom_url_generator_class'  => App\Services\MediaLibraryUrlGenerator::class,

    /*
     * The class that contains the strategy for determining a media file's path.
     */
    'custom_path_generator_class' => App\Services\MediaPathGenerator::class,

    's3'                          => [
        /*
         * The domain that should be prepended when generating urls.
         */
        'domain' => 'https://xxxxxxx.s3.amazonaws.com',
    ],

    'remote'                      => [
        /*
         * Any extra headers that should be included when uploading media to
         * a remote disk. Even though supported headers may vary between
         * different drivers, a sensible default has been provided.
         *
         * Supported by S3: CacheControl, Expires, StorageClass,
         * ServerSideEncryption, Metadata, ACL, ContentEncoding
         */
        'extra_headers' => [
            'CacheControl' => 'max-age=604800',
        ],
    ],
];

and here is my filsystems.phpfile :

<?php

return [

    /*
    |--------------------------------------------------------------------------
    | Default Filesystem Disk
    |--------------------------------------------------------------------------
    |
    | Here you may specify the default filesystem disk that should be used
    | by the framework. The "local" disk, as well as a variety of cloud
    | based disks are available to your application. Just store away!
    |
     */

    'default' => 'local',

    /*
    |--------------------------------------------------------------------------
    | Default Cloud Filesystem Disk
    |--------------------------------------------------------------------------
    |
    | Many applications store files both locally and in the cloud. For this
    | reason, you may specify a default "cloud" driver here. This driver
    | will be bound as the Cloud disk implementation in the container.
    |
     */

    'cloud'   => 's3',

    /*
    |--------------------------------------------------------------------------
    | Filesystem Disks
    |--------------------------------------------------------------------------
    |
    | Here you may configure as many filesystem "disks" as you wish, and you
    | may even configure multiple disks of the same driver. Defaults have
    | been setup for each driver as an example of the required options.
    |
    | Supported Drivers: "local", "ftp", "s3", "rackspace"
    |
     */

    'disks'   => [

        'local'  => [
            'driver' => 'local',
            'root'   => storage_path('app'),
        ],

        'public' => [
            'driver'     => 'local',
            'root'       => storage_path('app/public'),
            'visibility' => 'public',
        ],

        'media'  => [
            'driver' => 'local',
            'root'   => public_path('media'),
        ],

    ],

];

They are exactly the same as the ones working with Laravel. Maybe I did miss a namepsace conversion or something ?

kickthemooon commented 7 years ago

@loranger Having the same error. Did you manage to solve anything?

iraklisg commented 7 years ago

@loranger not sure if this is your case, but does your custom App\Services\MediaLibraryUrlGenerator correctly implements Spatie\MediaLibrary\UrlGenerator\UrlGenerator interface?

You can also try instead of implementing the interface, to extend the Spatie\MediaLibrary\UrlGenerator\BaseUrlGenerator

Please have a the docs as well

loranger commented 7 years ago

@kickthemooon @iraklisg I didn't find any way to resolve this issue and also had trouble to use the same User object with both Lumen and Laravel, so I dropped my lumen api and use Laravel only :/

Christophvh commented 6 years ago

I have the same issue and i'm not using Lumen.

benjivm commented 6 years ago

I am using v6.6.2 with Laravel 5.5 and cannot get this issue resolved.

Shandur commented 5 years ago

For those who comes here. My solution: check that your filesystems.php has configurations for 'disk_name' from medialibrary.php config file. Because media library relies on the value of disk_name and tries to create {DiskName}UrlGenerator class that probably doesn't exist.

Cheers!

bowetech commented 3 years ago

I got similar message, and after spending 1/2 a day i found out that my value in the media table under "disk" in the database had to match that of the value of your disk in your medialibray.php configuration file.

The error was misleading since it say that "Class Spatie\MediaLibrary\UrlGenerator\UrlGenerator doesn't exist"

In my case i had to change all the values in my table under the 'disk' field to media.