Laravel、Class控制器不存在于带前缀的路由中

Laravel, Class controller does not exist in route with prefixes

组(或回调)中的命名空间不起作用(Lumen,Laravel)?我想从 routes.php 中删除控制器的代码。所以!

use App\Http\Controllers;

// OK!
$app->get('path', 'BarController@getId');

$app->group(['prefix' => 'foo'], function ($app) {
    // OK!
    $app->get('path', '\App\Http\Controllers\BarController@getId');

    // Class BarController does not exist
    $app->get('path', 'BarController@getId');
});

要使 use 语句生效,您必须使用 ::class 语法:

use App\Http\Controllers\BarController;

$app->get('path', BarController::class.'@getId');

或者,您可以将命名空间添加到您的组中:

$app->group(['prefix' => 'foo', 'namespace' => 'App\Http\Controllers'], function ($app) {
    $app->get('path', 'BarController@getId');
});