【问题标题】:[PHP][LARAVEL] "Class 'App\User' not found" but there is clearly 'use App\User;' in the code[PHP][LARAVEL]“找不到类 'App\User'”,但显然有 'use App\User;'在代码中
【发布时间】:2019-10-17 03:03:17
【问题描述】:

我在 Laravel 的“api.php”中使用了一些非常简单的代码,这个错误让我发疯了。

代码:

<?php

use Illuminate\Http\Request;
use App\User;

// list new user
Route::get('users', function() {
    // If the Content-Type and Accept headers are set to 'application/json', 
    // this will return a JSON structure. This will be cleaned up later.
    return User::all();
});

// create new user
Route::post('users/add', function(Request $request) {
    return User::create($request->all);
});

错误:

找不到类“应用\用户”

这怎么可能?

【问题讨论】:

  • 您是否在App 命名空间中创建了User 类?
  • 如果您使用的是标准样板 Laravel 安装,则不会发生这种情况,因此分享我们可以用来重现此问题的任何代码可能很有用。
  • 尝试将App\User 更改为\App\User

标签: php laravel


【解决方案1】:

您好,您能否验证一下 User.php 模型顶部的命名空间是命名空间 App;?

类似:

<?php
namespace App;

use Illuminate\Notifications\Notifiable;`
use Illuminate\Notifications\MustVerifyEmail;

【讨论】:

    【解决方案2】:

    检查模型User.php中的命名空间。它应该是App\User

    【讨论】:

      【解决方案3】:

      确保运行composer update,然后运行composer dumpautoload

      【讨论】:

        【解决方案4】:

        尝试刷新现有的作曲家文件:

        composer dump-autoload
        

        另外,检查 composer.json 中的“自动加载”部分。

        【讨论】:

          猜你喜欢
          • 2016-01-23
          • 2021-12-28
          • 2017-06-07
          • 2017-10-04
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-01-08
          相关资源
          最近更新 更多