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); }); 
7
  • 1
    please read this stackoverflow.com/questions/63882034/… Commented Apr 5, 2021 at 5:27
  • thanks alot, but @KamleshPaul please check the updated part. then how to resolve it. Commented Apr 5, 2021 at 5:57
  • Have you also updated namespace ...; in the controller file after moving to the Api sub folder to reflect that? Commented Apr 5, 2021 at 6:00
  • yes i did it @msbit. "namespace App\Http\Controllers\Api;" Commented Apr 5, 2021 at 6: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? Commented Apr 5, 2021 at 7:13

2 Answers 2

0

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

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.