0

在带有 spatie/laravel-medialibrary 10 的 laravel 9 中,我尝试为查看文档的上传文件创建自定义路径:https ://spatie.be/docs/laravel-medialibrary/v9/advanced-usage/using-a-custom-directory -structure 但是制作 app/Services/MediaLibrary/CustomPathGenerator.php 文件:

<?php

namespace App\Services\MediaLibrary;
//namespace App\MediaLibrary;

use Spatie\MediaLibrary\MediaCollections\Models\Media;
use Spatie\MediaLibrary\PathGenerator\PathGenerator;

//class CustomPathGenerator implements BasePathGenerator
class CustomPathGenerator implements PathGenerator
{
    /*
     * Get the path for the given media, relative to the root storage path.
     */
    public function getPath(Media $media): string
    {
        return md5($media->id .  config('app.key')) .'/';
    }

    /*
     * Get the path for conversions of the given media, relative to the root storage path.
     */
    public function getPathForConversions(Media $media): string
    {
        return md5($media->id .  config('app.key')) .'/conversions/';

    }

    /*
     * Get the path for responsive images of the given media, relative to the root storage path.
     */
    public function getPathForResponsiveImages(Media $media): string {
        return md5($media->id .  config('app.key')) .'/responsive-images/';

    }
}

我得到错误:

[2022-02-16 17:52:44] local.ERROR: Interface "App\Services\MediaLibrary\BasePathGenerator" not found {"userId":1,"exception":"[object] (Error(code: 0): Interface \"App\\Services\\MediaLibrary\\BasePathGenerator\" not found at /mnt/_work_sdb8/wwwroot/lar/hostels4j/app/Services/MediaLibrary/CustomPathGenerator.php:8)

看起来我的文件头无效。

哪种方法有效?

提前致谢!

4

1 回答 1

0

您错误地定义了 BasePathGenerator 请改用以下内容

<?php

namespace App\Services\MediaLibrary;

use Spatie\MediaLibrary\MediaCollections\Models\Media;
use Spatie\MediaLibrary\Support\PathGenerator\PathGenerator as BasePathGenerator;

class CustomPathGenerator implements BasePathGenerator {
        /*
     * Get the path for the given media, relative to the root storage path.
     */
    public function getPath(Media $media): string {
        return '';
    }

    /*
     * Get the path for conversions of the given media, relative to the root storage path.
     */
    public function getPathForConversions(Media $media): string {
        return '';
    }

    /*
     * Get the path for responsive images of the given media, relative to the root storage path.
     */
    public function getPathForResponsiveImages(Media $media): string {
        return '';
    }
}

这应该可以解决您的问题。

于 2022-02-27T13:58:15.753 回答