简体   繁体   English

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

[英]Laravel 5 Class 'App\Http\Controllers\Category' not found

guys following the tuts tutorial for laravel , Since I have installed Laravel 5 couple of errors are throwing . 跟随laravel教程的家伙,由于我已经安装了Laravel,抛出了5条错误。

Code

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

This function is throwing two errors 该函数抛出两个错误

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

Well I know this is because of the different name spacing in Laravel 5 . 我知道这是由于Laravel 5中名称空间的不同。 for the 2nd error I tried to add 对于我尝试添加的第二个错误

use view at the begining of the file but it is not finding the view . 在文件开头use view ,但找不到该View。 Can any one let me know what directory these files resides 谁能让我知道这些文件所在的目录

Thanks 谢谢

Both of those are not in your Controllers. 这两个都不在您的控制器中。 The View class is apart of Illuminate and the other one is most likely a class Model you created. View类是Illuminate的一部分,另一个很可能是您创建的Model。

When you use a new class in a php file you must include it or use it. 当您在php文件中使用新类时,您必须包含或use它。

The way you would most likely do this for your Category class is" 您最有可能对Category类执行此操作的方式是“

use App\\Category that should solve that one. use App\\Category应解决该问题的use App\\Category

Your view one is only slightly more difficult. 您的看法仅稍微困难一点。 If you use an IDE where you can import classes that is much easier to do so you don't have to remember the namespace / class name. 如果您使用的IDE可以在其中导入更容易实现的类,则不必记住名称空间/类名称。 However, if you don't you need to know which one to use and when to use it. 但是,如果您不这样做,则需要知道使用哪个以及何时使用它。 In this case you will need to do: 在这种情况下,您需要执行以下操作:

use View this should solve this. use View这个应该可以解决这个问题。

So in your controller above the declaration and below your namespace you must put the use calls 因此,在声明上方和名称空间下方的控制器中,必须放置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 {
    ...
}

Like I mentioned in my comment there is a better way. 就像我在评论中提到的那样,有一种更好的方法。 You could just use the helper function view() to return the view just the same without doing the view the way you are doing it. 您可以只使用辅助函数view()来以相同的方式返回视图,而无需按照您的方式进行操作。

All you have to change is: 您只需更改:

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

to

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

Removes the confusion of messing up the classes. 消除了混淆类的混乱。

add following at top of file just below namespace declaration, namespace声明下方的文件顶部添加以下内容,

use Illuminate\Support\Facades\View;
use App\Category;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM