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,
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');
});
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.
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(){}
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With