Lumen: App \ Http \ Controllers \ Controller class not found with new installation

I am working with a new Lumen installation (creating a web API), most things work, but when I try to use a router to specify 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 /Http/routes.php

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

And this is my class in the application /Http/Controllers/MomentController.php

 <?php namespace App\Http\Controllers; use App\Http\Controllers\Controller; class MomentController extends Controller { public function index() { echo 123; } } 

I 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 this has something to do with the namespace, but I can't figure it out. Any clues?

THX

+5
source share
6 answers

The solution is to reference the right base controller so that it can propagate to this class.

 use Laravel\Lumen\Routing\Controller as BaseController; 

This line is the only thing I had to add for it to work.

So, the full class becomes the following:

 <?php namespace App\Http\Controllers; use Laravel\Lumen\Routing\Controller as BaseController; class ChannelController extends BaseController { public function getChannels(){} public function getChannel(){} } 
+2
source

Alas, none of them work. I can’t get a loan for a solution, but if you came here in search of a working answer, please refrain from this. Original post from Lukas Geiter here: Lumen frame routing not working

I changed the foo / bar example because really, who really likes this?


You must use the fully qualified class name:

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

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

 $app->group(['namespace' => 'App\Http\Controllers'], function($group){ $group->get('/', ' HomeController@index '); $group->get('users', ' UserController@index '); }); 
+4
source

I assume that you created the project using lumen new instead of composer create-project laravel/lumen --prefer-dist . You can try to create a new lumen project using a composer and try to reproduce this problem.

+1
source

Remove use App\Http\Controllers\Controller; since there is no need for this.

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

Alternatively, try composer du at the command line to reset and restore the startup file.

0
source

For someone else who is here with the same problem. I ran into the same problem while running my routes in my new Lumen 5.2 installation.

After hours of searching the Internet, it turns out that the Lumen route controller is different from the one used by Laravel. Lumen uses nikic fastroute.

The Lumen route controller does NOT support the route group prefix, even if it is specified in the documentation for Lumen. I don’t know if this was the original problem with the poster, as full route information is not available, but I hope it will save someone else a few hours.

I was unable to find the link if it is a function that needs to be enabled / added manually (if Lumen now supports it, as the documentation suggests). Maybe someone can shed some light on this?

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

0
source

try it

 $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 pointed out by @lowerends

-3
source

All Articles