laravel 流明:全新安装未找到 App\Http\Controllers\Controller 类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31871040/
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-09-14 12:08:28  来源:igfitidea点击:

lumen: App\Http\Controllers\Controller class not found with fresh install

phpclasslaravelrouterlumen

提问by kevinius

I'm working with a fresh installof Lumen (building a web API), most things work but when i'm trying to use the router to point to a class i get this error:

我正在使用全新安装的 Lumen(构建 Web API),大多数事情都可以工作,但是当我尝试使用路由器指向一个类时,我收到此错误:

Fatal error: Class 'App\Http\Controllers\Controller' not found in /Applications/MAMP/htdocs/moments/lumen/app/Http/Controllers/MomentController.php on line 5

This is my routerin app/Http/routes.php

这是我在 app/Http/routes.php 中的路由器

$app->get('/', 'MomentController@index');

And this is my classin app/Http/Controllers/MomentController.php

这是我在app / HTTP /控制器/ MomentController.php

<?php namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

class MomentController extends Controller {

    public function index()
    {
        echo 123;
    }

}

I have activated these components in bootstrap/app.php:

我已经在bootstrap/app.php 中激活了这些组件:

  • $app->withFacades();
  • $app->withEloquent();
  • Dotenv::load(__DIR__.'/../');
  • $app->withFacades();
  • $app->withEloquent();
  • Dotenv::load(__DIR__.'/../');

This is my composer.jsonfile:

这是我的composer.json文件:

{
    "name": "laravel/lumen",
    "description": "The Laravel Lumen Framework.",
    "keywords": ["framework", "laravel", "lumen"],
    "license": "MIT",
    "type": "project",
    "require": {
        "laravel/lumen-framework": "5.1.*",
        "vlucas/phpdotenv": "~1.0"
    },
    "require-dev": {
        "phpunit/phpunit": "~4.0",
        "fzaninotto/faker": "~1.0"
    },
    "autoload": {
        "psr-4": {
            "App\": "app/"
        },
        "classmap": [
            "database/"
        ]
    },
    "autoload-dev": {
        "classmap": [
            "tests/"
        ]
    },
    "config": {
        "preferred-install": "dist"
    }
}

I think it has something to do with the namespacing but i can't figure it out. Any clues?

我认为这与命名空间有关,但我无法弄清楚。有什么线索吗?

thx,

谢谢,

采纳答案by kevinius

The solution is to link to the right base controller so that it can extend of that class.

解决方案是链接到正确的基本控制器,以便它可以扩展该类。

use Laravel\Lumen\Routing\Controller as BaseController;

This line is the only thing i had to add in order to make it work.

这一行是我必须添加的唯一内容才能使其工作。

So the complete class becomes this:

所以完整的类变成了这样:

<?php namespace App\Http\Controllers;

use Laravel\Lumen\Routing\Controller as BaseController;

class ChannelController extends BaseController {

    public function getChannels(){}
    public function getChannel(){}

}

回答by Stan Smulders

Alas, none of these reliably work. I can't take credit for the solution but if you came here looking for a working answer please upvote this one. Original post by Lukas Geiter here: lumen framework routing not working

唉,这些都不能可靠地工作。我不能相信这个解决方案,但如果你来这里是为了寻找一个有效的答案,请给这个答案点赞。Lukas Geiter 的原帖在这里:流明框架路由不起作用

I did change the foo/bar example because really, who actually likes that?

我确实更改了 foo/bar 示例,因为真的,谁真的喜欢这样?



You have to use the fully qualified classname:

您必须使用完全限定的类名:

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

ORwrap all routes in a group (which is actually how it's done under the hood in Laravel 5)

将所有路线包装在一个组中(这实际上是 Laravel 5 中的幕后完成方式)

$app->group(['namespace' => 'App\Http\Controllers'], function($group){

    $group->get('/', 'HomeController@index');
    $group->get('users', 'UserController@index');

});

回答by 4unkur

I suppose that you've created a project using lumen newinstead of composer create-project laravel/lumen --prefer-dist. You may try to create a new lumen project using composer and try to reproduce this issue.

我想您已经使用lumen new而不是composer create-project laravel/lumen --prefer-dist. 您可以尝试使用 composer 创建一个新的 lumen 项目并尝试重现此问题。

回答by lowerends

Remove use App\Http\Controllers\Controller;as there's no need for that.

删除,use App\Http\Controllers\Controller;因为没有必要。

Then check if your composer.json has psr-4 enabled for the appdirectory.

然后检查您的 composer.json 是否为该app目录启用了 psr-4 。

Also, try a composer duon the command line to dump and regenerate the autoload file.

此外,尝试composer du在命令行上转储并重新生成自动加载文件。

回答by Danny

For anyone else who ended up here having the same issue. I experienced the same problem while stubbing out my routes within my fresh Lumen 5.2 installation.

对于最终在这里遇到同样问题的任何其他人。我在新安装的 Lumen 5.2 中删除路由时遇到了同样的问题。

After hours of searching the web it turns out that the route controller Lumen uses differs from the one Laravel uses. Lumen uses nikic fastroute.

经过数小时的网络搜索,结果证明 Lumen 使用的路由控制器与 Laravel 使用的路由控制器不同。Lumen 使用 nikic 快速路由。

The Lumen route controller does NOT support route group prefixes even though it is listed in the documentation for Lumen. I don't know if this was the original poster's problem as the full route details are not available but hopefully it will save someone else a few hours.

Lumen 路由控制器不支持路由组前缀,即使它列在 Lumen 的文档中。我不知道这是否是原始海报的问题,因为完整的路线详细信息不可用,但希望它可以为其他人节省几个小时。

I haven't been able to find any reference if this is a feature that needs to be enabled/added manually (if Lumen now supports it as the documentation suggest). Maybe someone can shed some light on this?

如果这是需要手动启用/添加的功能(如果 Lumen 现在按照文档建议支持它),我无法找到任何参考。也许有人可以对此有所了解?

https://lumen.laravel.com/docs/5.2/routing#route-group-prefixes

https://lumen.laravel.com/docs/5.2/routing#route-group-prefixes

回答by M0rtiis

try this

尝试这个

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

or (better) group them

或(更好)将它们分组

$app->group(['namespace' => 'App\Http\Controllers'], function($group){

    $group->get('/', 'MomentController@index');
    $group->get('foo', 'MomentController@otherAction');

});

and remove use App\Http\Controllers\Controller;as said @lowerends

并删除use App\Http\Controllers\Controller;如@lowerends所说