Lexx918
Lexx918

Reputation: 222

Laravel, Class controller does not exist in route with prefixes

Namespace in group (or callback) don't work (Lumen, Laravel)? I want to remove the code from routes.php to controllers. And so!

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');
});

Upvotes: 1

Views: 792

Answers (1)

Joseph Silber
Joseph Silber

Reputation: 219930

For your use statement to work, you'll have to use the ::class syntax:

use App\Http\Controllers\BarController;

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

Alternatively, you can add the namespace to your group:

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

Upvotes: 3

Related Questions