kevinius kevinius - 5 months ago 67
PHP Question

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,

Answer

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

}
Comments