Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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,

like image 584
kevinius Avatar asked Aug 07 '15 06:08

kevinius


4 Answers

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');

});
like image 102
Stan Smulders Avatar answered Oct 27 '22 17:10

Stan Smulders


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.

like image 28
lowerends Avatar answered Oct 27 '22 19:10

lowerends


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

}
like image 38
kevinius Avatar answered Oct 27 '22 18:10

kevinius


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.

like image 39
4unkur Avatar answered Oct 27 '22 19:10

4unkur