Laravel:如何编写资源控制器方法

时间:2017-03-15 09:00:23

标签: laravel controller resources

我写了以下内容:

public function show($id)
{
    $image = Image::find($id);
    $path = $image-file_path;
    return response()->file();
}

在routes / web.php中,我有以下内容:

Route::resource('image', 'ImageController');

现在我不知道为什么当我在浏览器中访问http://localhost:8000/image/5时,我得到:

FatalErrorException in ImageController.php line 48:
Class 'App\Http\Controllers\Image' not found
in ImageController.php line 48

我该怎么做才能解决这个问题?

4 个答案:

答案 0 :(得分:0)

这里有一个拼写错误$ path = $ image-file_path; 这应该是$ image-> file_path;

答案 1 :(得分:0)

您需要提供一个正确命名空间的路径来" Image"在线:

$image = Image::find($id);

然后根据Kostas'进行修复。建议所以它不会在下一行打破。

答案 2 :(得分:0)

问题1:未找到类'App \ Http \ Controllers \ Image'

您缺少在控制器中包含模型“图像

将模型包含在控制器的顶部:

例如:

use App\Image;

问题1:$ path = $ image-file_path;

Change form $path = $image-file_path;

$path = $image->file_path;

答案 3 :(得分:0)

您需要在控制器中包含图像模型

使用App \ Models \ Image;

或者如果您没有文件夹模型

使用App \ Image;