控制器 Class 未找到 - Lumen

Controller Class Not Found - Lumen

找不到我的 class 'Article' 控制器。

我需要从文章 table 中获取所有条目。

我可以使用 DB:: facade 来提取数据库中的内容,但是当我尝试使用 Article::all() 时,我得到:

Class 'App\Http\Controllers\Article' not found

in ArticleController.php line 15
at Application->Laravel\Lumen\{closure}()

第 15 行看起来像:

$article = Article::all();

这是我迄今为止尝试过的方法,但没有成功:

我的模型目录位于 app->Models 下并且有我的 Article.php 模型:

<?php

# app/Models/Article.php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Article extends Model
{
    protected $table = 'articles';
    protected $fillable = ['title', 'content'];
}

我的控制器是ArticleController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;


class ArticleController extends Controller{

    public function index(){

        $article = Article::all();

        return response()->json($article);
    }
}

还有我的routes.php

<?php

$app->get('article', 'ArticleController@index');

我非常感谢任何对此错误的帮助。不幸的是,我花了 2 天的大部分时间在这上面。

谢谢。

只是 运行 composer update。将生成一个新的自动加载类映射,并且您所有的新作曲家都将在成功更新后准备好自动加载。

您需要有正确的命名空间。您的 Article 模型位于 App\Models 命名空间中,因此您需要将其添加到控制器的顶部:

use App\Models\Article;

感谢您的回复和@Thomas 的回答。我听从了您的回复,并能够在我的控制器中添加正确的命名空间。