gpt4 book ai didi

php - 找不到 Laravel 5 类 'App\Http\Controllers\Category'

转载 作者:行者123 更新时间:2023-12-04 12:52:01 25 4
gpt4 key购买 nike

遵循 laravel 的 tuts 教程的人,因为我已经安装了 Laravel 5,所以抛出了几个错误。

代码

public function getIndex() {
return View::make('categories.index')
->with('categories', Category::all());
}

这个函数抛出两个错误

Class 'App\Http\Controllers\Category' not found
Class 'App\Http\Controllers\views' not found

好吧,我知道这是因为 Laravel 5 中的名称间距不同。对于我尝试添加的第二个错误

在文件开头使用 View ,但未找到 View 。谁能告诉我这些文件所在的目录

谢谢

最佳答案

这两个都不在您的 Controller 中。 View 类是 Illuminate 的一部分,另一个很可能是您创建的类 Model。

当您在 php 文件中使用新类时,您必须包含它或使用它。

您最有可能为您的类别类执行此操作的方式是“

使用 App\Category 应该可以解决这个问题。

您的观点只是稍微困难一些。如果您使用的 IDE 可以导入更容易的类,那么您就不必记住命名空间/类名。但是,如果您不这样做,则需要知道使用哪一个以及何时使用它。在这种情况下,您需要执行以下操作:

使用 View 这应该可以解决这个问题。

所以在您的 Controller 中,声明上方和命名空间下方您必须放置 use 调用

<?php namespace App\Http\Controllers

// I'm removing this because there is a way not to even have to use this...
// use View;
use App\Category;

class YourController extends Controller {
...
}

就像我在评论中提到的,有更好的方法。您可以只使用辅助函数 view() 来返回相同的 View ,而无需按照您正在执行的方式进行 View 。

所有你需要改变的是:

return View::make('categories.index')->with('categories', Category::all());

return view('categories.index', ['categories' => Category::all()]);
or
return view('categories.index')->with('categories',Category::all())

消除了混淆类的困惑。

关于php - 找不到 Laravel 5 类 'App\Http\Controllers\Category',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31454101/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com