ReflectionException Controller 在 Lumen 中不存在错误

ReflectionException Controller does not exist error in Lumen

我在 App\Http\Controllers\Controller.php

中制作控制器

我使用路由跟随代码

$app->get('api/article','App\Http\Controllers\ArticleController@index');

但是我不能调用Controller! 控制器不存在..... 发生错误。 怎么解决?

您只需要指定相对于App\Http\Controllers的命名空间。所以它会是这样的:

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

此外,为了将来参考,如果您的控制器位于 "deeper" 命名空间中,则适用相同的规则。所以,如果你的 ArticlesControllerApp\Http\Controllers\API\ArticleController 中,你只需要这样做:

$app->get('api/article', 'API\ArticleController@index');

It is very important to note that we did not need to specify the full controller namespace when defining the controller route. We only defined the portion of the class name that comes after the App\Http\Controllers namespace "root". By default, the bootstrap/app.php file will load the routes.php file within a route group containing the root controller namespace.

If you choose to nest or organize your controllers using PHP namespaces deeper into the App\Http\Controllers directory, simply use the specific class name relative to the App\Http\Controllers root namespace. So, if your full controller class is App\Http\Controllers\Photos\AdminController, you would register a route like so:

$app->get('foo', 'Photos\AdminController@method');

来源:http://lumen.laravel.com/docs/controllers#basic-controllers