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

江枫思渺然 提交于 2020-01-03 12:33:12

问题


I'm working with a fresh install of 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:

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 router in app/Http/routes.php

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

And this is my class in app/Http/Controllers/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:

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

This is my composer.json file:

{
    "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,


回答1:


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

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


You have to use the fully qualified classname:

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

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

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

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

});



回答2:


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(){}

}



回答3:


I suppose that you've created a project using lumen new instead 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.




回答4:


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

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

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




回答5:


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.

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.

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.

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?

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




回答6:


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



来源:https://stackoverflow.com/questions/31871040/lumen-app-http-controllers-controller-class-not-found-with-fresh-install

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!