5

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,

lowerends
  • 5,469
  • 2
  • 24
  • 36
kevinius
  • 4,232
  • 7
  • 48
  • 79
  • But isn't the Controller class an internal class that lumen should load? I have only 1 file and 1 class in my app/Http/Controllers/ folder (MomentsController.php & MomentsController class). Thx for your time... – kevinius Aug 07 '15 at 06:41
  • If i remove "extends Controller" then i get the echo 123 outputted and no errors. So it's clearly not finding the base controller class. – kevinius Aug 07 '15 at 06:52

6 Answers6

5

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

});
Community
  • 1
  • 1
Stan Smulders
  • 6,079
  • 1
  • 27
  • 23
  • For some reason this answer gets as many downvotes as upvotes. If you downvote, please explain why! Maybe It'll help others :) – Stan Smulders Mar 14 '16 at 08:10
2

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.

lowerends
  • 5,469
  • 2
  • 24
  • 36
  • Ok, updated the answer with some extra possible things to check/try. – lowerends Aug 07 '15 at 06:53
  • I changed my post to add the composer.json data. I also did a composer dump-autoload but still without luck. – kevinius Aug 07 '15 at 06:59
  • Hmm, ok, the only thing I can think of then is to try and remove `$app->withFacades();` in `app.php` to see if that fixes the problem. – lowerends Aug 07 '15 at 07:25
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(){}

}
kevinius
  • 4,232
  • 7
  • 48
  • 79
  • Nice, you helped me out with this on your widequestion response! (for those who didn't see it, they are identical responses) – Cody Wikman Jul 07 '17 at 22:46
2

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.

4unkur
  • 827
  • 9
  • 14
0

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

Danny
  • 21
  • 2
-3

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

M0rtiis
  • 3,676
  • 1
  • 15
  • 22
  • Now i get: Class App\Http\Controllers\App\Http\Controllers\MomentController does not exist – kevinius Aug 07 '15 at 06:51
  • 1
    You don't wanna put whole namespace path for your controllers as Lavael already set up to treat controller's namespace to `App\Http\Controllers` – Almazik G Aug 07 '15 at 07:28