我是Laravel的新手,文档的基本任务列表从Route(web.php
)返回视图,但是我想使用Controller返回图像文件。
我有自己的路线:
Route::get('/products', 'ProductController@index');
然后执行我的ProductController动作(请在我使用index
简化操作时忽略注释)
<?php
namespace App\Http\Controllers;
use App\Product;
use Illuminate\Http\Request;
class ProductController extends Controller
{
/**
* Display a listing of the resource.
*
@return \Illuminate\Http\Response
Fetch and return all product records.
*/
public function index()
{
//
//return response()->json(Product::all(), 200);
return view('/pages/product', compact('product'));
}
还有我的product.blade.php(位于视图/页面/产品中):
<img src="/images/product/Frozen_Ophelia_800x.png">
我不断收到ReflectionException类App \ Product不存在。
当我刚刚从路线返回一个视图时,我就开始工作了。我得到一个ReflectionException
Class App\Product does not exist
,所以我认为它是最重要的,即。 use App\Product;
是错误的。
编辑(下面是我的App\Product
嵌套在app/Providers
中):
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Product extends Model
{
//
use SoftDeletes
protected $fillable = [
'name', 'price', 'units', 'description', 'image'
];
public function orders(){
return $this->hasMany(Order::class);
}
}
答案 0 :(得分:2)
假设存在App\Product
模型,则正确的代码应为:
public function index() {
$product = Product::all();
return view('pages.product', compact('product'));
}
选中the docs。
PS您打过$ composer dumpautoload
吗? ReflectionException Class
错误通常与自动加载新类有关(例如,程序包中的新类)
答案 1 :(得分:1)
view函数应该具有任何视图模板,而不是任何url或路由。您有views/pages/product.blade.php
个文件,然后使用
view('pages.product',compact('product'));