14

I use the Lumen framework for first time, the route / to my HomeController is not working.

This is my route.php:

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

But I get the following error:

[2015-04-17 07:03:41] lumen.ERROR: exception 'ReflectionException' with message 'Class HomeController does not exist' in /Users/refear99/Web/qingsongchou_api/vendor/illuminate/container/Container.php:776

Stack trace:
#0 /Users/refear99/Web/qingsongchou_api/vendor/illuminate/container/Container.php(776): ReflectionClass->__construct('HomeController')
#1 /Users/refear99/Web/qingsongchou_api/vendor/illuminate/container/Container.php(656): Illuminate\Container\Container->build('HomeController', Array)
#2 /Users/refear99/Web/qingsongchou_api/vendor/laravel/lumen-framework/src/Application.php(358): Illuminate\Container\Container->make('HomeController', Array)
#3 /Users/refear99/Web/qingsongchou_api/vendor/laravel/lumen-framework/src/Application.php(1184): Laravel\Lumen\Application->make('HomeController')
#4 /Users/refear99/Web/qingsongchou_api/vendor/laravel/lumen-framework/src/Application.php(1157): Laravel\Lumen\Application->callControllerAction(Array)
#5 /Users/refear99/Web/qingsongchou_api/vendor/laravel/lumen-framework/src/Application.php(1142): Laravel\Lumen\Application->callActionOnArrayBasedRoute(Array)
#6 /Users/refear99/Web/qingsongchou_api/vendor/laravel/lumen-framework/src/Application.php(1120): Laravel\Lumen\Application->handleArrayBasedFoundRoute(Array)
#7 /Users/refear99/Web/qingsongchou_api/vendor/laravel/lumen-framework/src/Application.php(1058): Laravel\Lumen\Application->handleFoundRoute(Array)
#8 /Users/refear99/Web/qingsongchou_api/vendor/laravel/lumen-framework/src/Application.php(1006): Laravel\Lumen\Application->dispatch(NULL)
#9 /Users/refear99/Web/qingsongchou_api/public/index.php(28): Laravel\Lumen\Application->run()
#10 {main}  

This is my HomeController.php in /app/Http/Controllers/

<?php namespace App\Http\Controllers;

class HomeController extends Controller {

public function index()
{
    echo 123;
}

}

What could the problem be?

Oldskool
  • 34,211
  • 7
  • 53
  • 66
refear99
  • 1,217
  • 3
  • 10
  • 13

3 Answers3

45

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('foo', 'FooController@index');

});
lukasgeiter
  • 147,337
  • 26
  • 332
  • 270
  • 1
    This is strange, because if you look into https://github.com/laravel/lumen/blob/master/bootstrap/app.php#L97 there is already namespace option, but it does not work - hey wait it works but not in nested groups :( – Dusan Plavak Jul 02 '16 at 16:53
  • The docs that say the exact opposite are mildly confusing. https://lumen.laravel.com/docs/5.2/controllers#basic-controllers – Dylan Buth Jul 13 '16 at 15:33
  • @Helliax This answer was definitely correct when I wrote it (Lumen 5.0). It's very much possible that this has been changed in one of the newer releases. I don't have time to try it out right now, but if somebody can confirm that it actually changed, I'm going to update my answer. – lukasgeiter Jul 13 '16 at 16:54
  • 1
    @lukasgeiter you're answer is still correct. My comment was more in the line with skcin7. Doesn't make sense that the docs say the opposite of what actually works. – Dylan Buth Jul 13 '16 at 17:01
  • It seems that there is a typo. $group should be $app otherwise $app will not be passed into the closure and $group will be an undefined variable. Other than that, works with a smile. Thanks :) – andy_roddam Sep 09 '16 at 10:33
2

It appears to be undocumented right now, but you need to use the full namespace path to the controller.

So your route would look like this:

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

The difference lies in the RouteServiceProvider that ships with Laravel, which can be found in app/Providers/RouteServiceProvider.php, check out the map method, it looks as follows

$router->group(['namespace' => $this->namespace], function($router)
{
    require app_path('Http/routes.php');
});

So all of your application routes are actually grouped under a default namespace, which is usually App\Http\Controllers.

Hope that helps!

0

Take a look at the file /bootstrap/app.php There you can make some settings. Also there, at the bottom of the file, you will find the following lines.

$app->group(['namespace' => 'App\Http\Controllers'], function ($app) {
    require __DIR__.'/../app/Http/routes.php';
});

return $app;

Which should serve your calls with the right namespace.

Also you can activate the .env settings right there :)

Take a look at this Post https://mattstauffer.co/blog/introducing-lumen-from-laravel

Hope this helps someone! :)

Thomas Venturini
  • 3,500
  • 4
  • 34
  • 43