php Laravel 5 - FatalErrorException:找不到类“用户”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28610771/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:59:15  来源:igfitidea点击:

Laravel 5 - FatalErrorException: Class 'User' not found

phpcomposer-phplaravel-5

提问by Rajeeb

I am very new to Laravel and just started setting up things with Laravel 5. I am attempting to create a simple user authentication app with Laravel.

我对 Laravel 非常陌生,刚刚开始使用 Laravel 5 进行设置。我正在尝试使用 Laravel 创建一个简单的用户身份验证应用程序。

I created register.blade.php which includes form to register user.

我创建了 register.blade.php,其中包括用于注册用户的表单。

Here is my routes.php

这是我的routes.php

Route::post('/register', function()
{
$user = new User;  
$user->email = Input::get('email');
$user->username = Input::get('username');
$user->password = Hash::make(Input::get('password'));
$user->save();
$theEmail = Input::get('email');
return View::make('thanks')->with('theEmail', $theEmail);
});

Here is the section of register.blade.php which creates form for the user registration.

这是为用户注册创建表单的 register.blade.php 部分。

{!! Form::open(array('url' => 'register')) !!}
       {!! Form::label('email', 'Email Address') !!}
       {!! Form::text('email') !!}

       {!! Form::label('username', 'Username') !!}
       {!! Form::text('username') !!}

       {!! Form::label('password', 'Password') !!}
       {!! Form::password('password') !!}

       {!! Form::submit('Sign Up') !!}
{!! Form::close() !!}

I am getting this error when I click on Sign Up button on register page.

单击注册页面上的注册按钮时出现此错误。

in routes.php line 30 at HandleExceptions->fatalExceptionFromError(array('type' => '1', 'message' => 'Class 'User' not found', 'file' => 'C:\xampp\htdocs\urlshort\app\Http\routes.php', 'line' => '30')) in HandleExceptions.php line 116 at HandleExceptions->handleShutdown()

在 HandleExceptions->fatalExceptionFromError(array('type' => '1', 'message' => 'Class 'User' not found', 'file' => 'C:\xampp\htdocs\ urlshort\app\Http\routes.php', 'line' => '30')) 在 HandleExceptions.php 第 116 行,在 HandleExceptions->handleShutdown()

After going through few queries in Google, I realized I forgot to load the User class. So, I included link to file with User class in composer.json file

在 Google 中进行了几次查询后,我意识到我忘记加载 User 类。所以,我在 composer.json 文件中包含了带有 User 类的文件链接

    "autoload": {
    "classmap": [
        "database",
        "app/User.php"
    ],
    "psr-4": {
        "App\": "app/"
    }
},

I ran the composer dump-autoloadcommand. But I am still getting the same error. I am unable to figure out where my code went work.

我运行了composer dump-autoload命令。但我仍然遇到同样的错误。我无法弄清楚我的代码在哪里工作。

回答by xcerx

try to use

尝试使用

$user = new \App\User;

$user = new \App\User;

instead

反而

$user = new User;

$user = new User;

回答by Patrick Mutwiri

I had the same issue, the answer above didn't help, but I used

我有同样的问题,上面的答案没有帮助,但我用过

use App\Models\Access\User\User;instead of use User;in my controller and it worked.

use App\Models\Access\User\User;而不是use User;在我的控制器中,它起作用了。