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); });
namespace ...;in the controller file after moving to theApisub folder to reflect that?ProductController.phpmatches the correspondinguse ...inroutes/api.php, then it should be found. Is it exactly the same error?