0

I'm using laravel 8.35.1 version. I have a api-resource controller "ProductController". At my route file api.php. I define the route this:

api.php

<?php

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;


Route::middleware('auth:api')->get('/user', function (Request $request) {
    return $request->user();
});

Route::apiResource('/products', 'App\Http\Controllers\ProductController');

Route::group(['prefix' => 'products'], function () {
    Route::apiResource('/{product}/review', 'App\Http\Controllers\ReviewController');
});

NOTE: It's work fine but, when i remove the complete path of controller like just write Route::apiResource('/products', 'ProductController'); it show error

Target class [ProductController] does not exist.

Before first clearing the cache. I want to get rid of the complete path. and second want to place the controllers in Api folder, so how to define route for that also. I have also tried ProductController::class but not work fine

Updated when I use the route according the laravel 8 doc. https://laravel.com/docs/8.x/controllers#resource-controllers it is working fine. but when move the controller file to Api folder then declare the route name space like use App\Http\Controllers\Api\ProductController; show error again

<?php

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ProductController;
use App\Http\Controllers\ReviewController;

Route::middleware('auth:api')->get('/user', function (Request $request) {
    return $request->user();
});

Route::apiResource('/products', ProductController::class);

Route::group(['prefix' => 'products'], function () {
    Route::apiResource('/{products}/reviews', ReviewController::class);
});
shaedrich
  • 5,457
  • 3
  • 26
  • 42
Mukhlis Raza
  • 211
  • 1
  • 3
  • 13
  • 1
    please read this https://stackoverflow.com/questions/63882034/target-class-does-not-exist-problem-in-laravel-8/63882104#63882104 – Kamlesh Paul Apr 05 '21 at 05:27
  • thanks alot, but @KamleshPaul please check the updated part. then how to resolve it. – Mukhlis Raza Apr 05 '21 at 05:57
  • Have you also updated `namespace ...;` in the controller file after moving to the `Api` sub folder to reflect that? – msbit Apr 05 '21 at 06:00
  • yes i did it @msbit. "namespace App\Http\Controllers\Api;" – Mukhlis Raza Apr 05 '21 at 06:03
  • If the namespace in `ProductController.php` matches the corresponding `use ...` in `routes/api.php`, then it should be found. Is it exactly the same error? – msbit Apr 05 '21 at 07:13
  • @msbit yes exactly the same error – Mukhlis Raza Apr 05 '21 at 07:15
  • I'm not sure what it is then, it may be helpful to update the question with your current `routes/api.php` and `app/Http/Controllers/Api/ProductController.php` files. – msbit Apr 05 '21 at 07:32

2 Answers2

0

You dont have to use class on declare controller in routing. You may change 'ProductController' instead ProductController::class

hery
  • 32
  • 1
0

When i try this then it work fine for me.

<?php

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ReviewController;

Route::middleware('auth:api')->get('/user', function (Request $request) {
    return $request->user();
});

Route::apiResource('/products', 'App\Http\Controllers\ProductController');

Route::group(['prefix' => 'products'], function () {
    Route::apiResource('/{product}/reviews', [ReviewController::class, 'ReviewController']);
});
Muqadar Ali
  • 57
  • 2
  • 11