0

while creating a route, when I wanted to use a group, I stopped seeing the controller. And I have no idea what is wrong since it is in this folder and it works normally when it calls it Route::apiResource. Does anyone know what I'm doing wrong?

Route Code:

Route::group(['prefix' => 'v1', 'namespace' => 'App\Http\Controllers\Api\v1'], function(){
    Route::prefix('news')->group(function(){
        Route::get('/data?getPosts', [NewsController::class, 'index']); - Not Works
        Route::get('/data', [NewsController::class, 'index']);  - Not Works
    });
    //Route::apiResource('news', NewsController::class);  - Works
});

Error:

Target class [NewsController] does not exist.
Porjoton
  • 1
  • 1
  • Did you add `use App\Http\Controllers\NewsController;` to the top of your file? (I assume you did, but can't see it in your question) – Tim Lewis Jul 19 '22 at 21:15
  • @TimLewis to the file from route no. I have a NewsController in the Http/Controllers/Api/v1 folder and when I use this apiResource, it works for me, but with this prefix and groups, I do not need to create routines myself, for example, for create to be created – Porjoton Jul 19 '22 at 21:20
  • Interesting; I'm not familiar with the `'namespace' => '...'` part of `Route::group()`; I haven't used it before, but I'll look into it's usage and try to recreate your issue (and maybe someone else will be able to help in the meantime). Also, what's the Laravel version? – Tim Lewis Jul 19 '22 at 21:23
  • 1
    PHP 8.0.14 Laravel 9.19.0 – Porjoton Jul 19 '22 at 21:30
  • So I should use it for rest-api use App\Http\Controllers\...; any time? With a large number of controllers, there will be a mass of it in the routing file – Porjoton Jul 19 '22 at 21:55
  • I would recommend that, yes: https://stackoverflow.com/questions/7121682/php-how-to-import-all-classes-from-another-namespace. If you have a handful of Controllers, a `use ...;` statement for each isn't bad/hard to maintain, but for larger amounts of classes, just import them their namespace and reference accordingly. – Tim Lewis Jul 19 '22 at 23:01

2 Answers2

0

If you want to use the namespace prefix you can't use the "callable" syntax for the route action. You will need to use the string version, Class@method:

Route::group(['namespace' => 'App\Http\Controllers\Api\v1', ...], function () {
    Route::get(..., 'NewsController@index');
});

The callable syntax, ['Class', 'method'], expects the FQCN and overrides any namespace defined in a group (does not cascade) or so it seems; which makes sense since that is how callables work.

lagbox
  • 48,571
  • 8
  • 72
  • 83
-1

The namespace option has been broken for a long time. Do not use it.

use App\Http\Controllers\Api\v1\NewsController;

Route::group(['prefix' => 'v1'], function () {
    Route::prefix('news')->group(function () {
        Route::get('/data?getPosts', [NewsController::class, 'index']);
        Route::get('/data', [NewsController::class, 'index']);
    });

    Route::apiResource('news', NewsController::class);
});
IGP
  • 14,160
  • 4
  • 26
  • 43