【发布时间】:2021-08-16 15:56:15
【问题描述】:
在下面的路由中,检查该方法返回的图像路径,它们都以“/categories/”开头但这是不对的,实际路径应该是/public/images/categories/gallery/1而不是/public/categories/images/categories/gallery/1
Route::get('/categories/{group:id}', function (Group $group) {
$images = Storage::disk('public_path')->files('images/categories/gallery/' . $group->id);
return view('categories.index', ['group' => $group], compact('images'));
})->name('categories');
以下是实际获取正确路径的路径示例
Route::get('/news', function () {
$categories = Category::get();
$images = Storage::disk('public_path')->files('images/news/gallery');
return view('news', ['categories' => $categories], compact('images'));
})->name('news');
所以我的问题是,为什么它在前面的路线中,它返回的路径不正确,/categories/ 附加到开头但不在上面,我如何在发布的第一个代码示例中获得正确的路径.
我尝试将路由更改为Route::get('/{group:id}', function ... etc,它可以工作,但我无法使用它,因为 url 子目录只是一个数字。
我还尝试在 files() 方法中的路径中添加一个正斜杠,因此它是 ->files('/images/categories/gallery/1');,但它仍将 /categories/ 附加到开头。
这是filesystems.php中方法的配置
'disks' => [
'local' => [
'driver' => 'local',
'root' => storage_path('app'),
],
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
],
'public_path' => [
'driver' => 'local',
'root' => public_path(''),
],
's3' => [
'driver' => 's3',
'key' => env('AWS_ACCESS_KEY_ID'),
'secret' => env('AWS_SECRET_ACCESS_KEY'),
'region' => env('AWS_DEFAULT_REGION'),
'bucket' => env('AWS_BUCKET'),
'url' => env('AWS_URL'),
'endpoint' => env('AWS_ENDPOINT'),
],
],
【问题讨论】:
-
我采用的临时解决方法是使用
'/category_group_{group:id}'作为路径,但仍然对此感到疑惑。 -
代码对我来说看起来不错。我试图在我的机器上重现您的错误,但不能。你的 laravel 版本是什么?
-
"laravel/framework": "^8.12"检查元素时,它会显示正确的路径以及print_r($images)中的路径,但将鼠标悬停在src属性上是当它在工具提示中附加/categories/时,图像全部有损坏的链接图标。