You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel访问个人主页路由报错Class 'App\User' not found求助

问题描述

如果我有不符合发帖规范的地方在此提前致歉,说明一下,我是一名PHP开发新手。
我目前正在学习freecodecamp发布的Instagram克隆项目教程,教程名称为 Laravel PHP Framework Tutorial - Full Course for Beginners (2019),当前学习进度为视频时间戳1:08:30 - 1:09:30段落。在修改ProfilesController实现个人主页用户名动态展示功能之前,项目所有功能运行正常,修改后出现报错。
以下是报错回溯中引用的相关代码片段:

报错信息

Error
Class 'App\User' not found
访问地址:http://localhost:8000/profile/1
报错位置:App\Http\Controllers\ProfilesController::index
对应文件:C:\Users\Blake\freeCodeGram\app\Http\Controllers\ProfilesController.php 第13行

相关代码

app/Http/Controllers/ProfilesController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class ProfilesController extends Controller
{
    public function index($user)
    {
        \App\User::find($user);

        return view('home',[
            'user' => $user,
        ]);
    }
}

routes/web.php

<?php

use Illuminate\Support\Facades\Route;

Route::get('/', function () {
    return view('welcome');
});

Auth::routes();

Route::get('/profile/{user}', [App\Http\Controllers\ProfilesController::class, 'index'])->name('profile.show');

app/Models/User.php

namespace App\Models;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;

class User extends Authenticatable
{
    use HasApiTokens, HasFactory, Notifiable;

    /**
     * 可批量赋值属性
     *
     * @var array<int, string>
     */
    protected $fillable = [
        'name',
        'email',
        'username',
        'password',
    ];

    /**
     * 序列化时隐藏的属性
     *
     * @var array<int, string>
     */
    protected $hidden = [
        'password',
        'remember_token',
    ];

    /**
     * 属性类型转换
     *
     * @var array<string, string>
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];
}

感谢大家的帮助!


报错原因
  • 你使用的Laravel 8及以上版本,默认将所有模型类放在App\Models命名空间下。你参考的2019年教程基于更早的Laravel版本,当时User模型直接存放在App根命名空间,因此控制器中写\App\User::find($user)会找不到对应类。
  • 现有代码还有一个逻辑问题:调用find方法查询到的用户实例没有赋值给变量,最终传给视图的是路由传入的用户ID数字,后续视图调用用户属性(比如用户名)会直接报错。
修复方案
  1. 基础修复:修改ProfilesController.php,引入正确命名空间的User模型,将查询到的用户实例传给视图:
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
// 引入正确命名空间的User模型
use App\Models\User;

class ProfilesController extends Controller
{
    public function index($user)
    {
        // 查询对应用户,不存在直接返回404
        $user = User::findOrFail($user);

        return view('home', compact('user'));
    }
}
  1. 优化写法(推荐):使用Laravel自带的路由模型绑定,省去手动查询逻辑。控制器方法直接注入User模型,Laravel会自动根据路由参数查询对应记录,不存在则自动返回404:
<?php

namespace App\Http\Controllers;

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

class ProfilesController extends Controller
{
    // 直接注入User模型,参数名和路由参数{user}一致即可自动绑定
    public function index(User $user)
    {
        return view('home', compact('user'));
    }
}

路由部分不需要修改,保持原有写法即可正常运行。


内容的提问来源于stack exchange,提问作者Blake

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.29 13:27:32