takaya030の備忘録

PHP、Laravel、Docker などの話がメインです

Lumen の Controller の namespace について

Lumen 5.2 でコントローラのルートの namespace が App\Http\Controllers に変更されていたのでメモ

Lumen 5.1 で動いていたコードが 5.2 でエラーになった件

app/Http/routes.php

<?php

$app->get('/', [
	'uses' => 'App\Http\Controllers\WelcomeController@index',
]);

上のコードを Lumen の最新版(5.2)で実行したとき発生したエラー

[2016-06-25 04:01:55] lumen.ERROR: ReflectionException: Class App\Http\Controllers\App\Http\Controllers\WelcomeController does not exist in /webapp/vendor/illuminate/container/Container.php:734

Lumen 5.1 では Controller のルートは namespace 無しだったが、5.2 では 'App\Http\Controllers' になったらしい。
以下のように変更したら無事動きました。

<?php

$app->get('/', [
	'uses' => 'WelcomeController@index',
]);

ちなみに Controller のデフォルト namespace は bootstrap/app.php で定義されている。
Lumen 5.1 の bootstrap/app.php 84行目

<?php

/*
|--------------------------------------------------------------------------
| Load The Application Routes
|--------------------------------------------------------------------------
|
| Next we will include the routes file so that they can all be added to
| the application. This will provide all of the URLs the application
| can respond to, as well as the controllers that may handle them.
|
*/

require __DIR__.'/../app/Http/routes.php';

Lumen 5.2 の bootstrap/app.php 85行目

<?php

/*
|--------------------------------------------------------------------------
| Load The Application Routes
|--------------------------------------------------------------------------
|
| Next we will include the routes file so that they can all be added to
| the application. This will provide all of the URLs the application
| can respond to, as well as the controllers that may handle them.
|
*/

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

検証した Lumen のバージョン

Lumen 5.1

$ php artisan --version
Laravel Framework version Lumen (5.1.6) (Laravel Components 5.1.*)

Lumen 5.2

$ php artisan --version
Laravel Framework version Lumen (5.2.7) (Laravel Components 5.2.*)