1

Fatal error: Uncaught Error: Class "App\Http\Controllers\Controller" not found in C:\Users\krithu\livechat\laravelapi\laravelbookstoreapi\bookstoreapi\bookstore\app\Http\Controllers\AuthorsController.php:10 Stack trace: #0 {main} thrown in C:\Users\krithu\projecrrepository\laravelapi\laravelbookstoreapi\bookstoreapi\bookstore\app\Http\Controllers\AuthorsController.php on line 10

Below is my Controller.php

<?php

namespace App\Http\Controllers;

use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller as BaseController;

class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
}

Below is my api.php

Route::middleware('auth:api')->prefix('v1')->group(function() {
    Route::get('/user', function(Request $request){
        return $request->user();
    });
   
    Route::apiResource('/authors', AuthorsController::class);
   
});

Below is my AuthorsController.php

<?php

namespace App\Http\Controllers;

use App\Models\Author;
use App\Http\Requests\StoreAuthorRequest;
use App\Http\Requests\UpdateAuthorRequest;
use App\Http\Resources\AuthorsResource;

class AuthorsController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return AuthorsResource::collection(Author::all());
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \App\Http\Requests\StoreAuthorRequest  $request
     * @return \Illuminate\Http\Response
     */
    public function store(StoreAuthorRequest $request)
    {

        return 'Test';
       /*  $author = Author::create([
            'name' => 'John Doe'
        ]);
        return new AuthorsResource($author); */
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Models\Author  $author
     * @return \Illuminate\Http\Response
     */
    public function show(Author $author)
    {
       // return $author;
       return new AuthorsResource($author);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\Author  $author
     * @return \Illuminate\Http\Response
     */
    public function edit(Author $author)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \App\Http\Requests\UpdateAuthorRequest  $request
     * @param  \App\Models\Author  $author
     * @return \Illuminate\Http\Response
     */
    public function update(UpdateAuthorRequest $request, Author $author)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\Author  $author
     * @return \Illuminate\Http\Response
     */
    public function destroy(Author $author)
    {
        //
    }
}

Below is my RouteServiceProver.php

<?php

namespace App\Providers;

use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Support\Facades\Route;

class RouteServiceProvider extends ServiceProvider
{
    /**
     * The path to the "home" route for your application.
     *
     * This is used by Laravel authentication to redirect users after login.
     *
     * @var string
     */
    public const HOME = '/home';

    /**
     * The controller namespace for the application.
     *
     * When present, controller route declarations will automatically be prefixed with this namespace.
     *
     * @var string|null
     */
    // protected $namespace = 'App\\Http\\Controllers';

    /**
     * Define your route model bindings, pattern filters, etc.
     *
     * @return void
     */
    public function boot()
    {
        $this->configureRateLimiting();

        $this->routes(function () {
            Route::prefix('api')
                ->middleware('api')
                ->namespace($this->namespace)
                ->group(base_path('routes/api.php'));

            Route::middleware('web')
                ->namespace($this->namespace)
                ->group(base_path('routes/web.php'));
        });
    }

    /**
     * Configure the rate limiters for the application.
     *
     * @return void
     */
    protected function configureRateLimiting()
    {
        RateLimiter::for('api', function (Request $request) {
            return Limit::perMinute(60)->by(optional($request->user())->id ?: $request->ip());
        });
    }
}

I am doing a post request http://127.0.0.1:8000/api/v1/authors As per the route list it should execute the store method and return an output of Test.

krithupayal
  • 21
  • 1
  • 4
  • fxi, the error appears in `AuthorsController.php` – brombeer Jan 21 '22 at 08:57
  • @brombeer-> I updated now. Can you please help me? – krithupayal Jan 21 '22 at 09:32
  • "Below is my `Controllers.php`" ... you mean `Controller.php`, right? since the class is named `Controller` not `Controllers` – lagbox Jan 21 '22 at 23:51
  • Thanks for responding, It's Controller.php only in my vscode. Some how my error is changes now after making false to true for authentication.TypeError: Illuminate\Validation\Factory::make(): Argument #2 ($rules) must be of type array, App\Http\Resources\AuthorsResource given, called in projecrrepository\livechat\laravelapi\laravelbookstoreapi\bookstoreapi\bookstore\vendor\laravel\framework\src\Illuminate\Foundation\Http\FormRequest.php on line 114 in file laravelbookstoreapi\bookstoreapi\bookstore\vendor\laravel\framework\src\Illuminate\Validation\Factory.php on line 105 – krithupayal Jan 23 '22 at 11:09
  • can some one from stackover help me here wating from 2 days :( – krithupayal Jan 23 '22 at 11:10
  • I am not super familiar with PHP, but are you importing Controller properly? You define it in `Controller.php` and use it in `AuthorsController.php`, but I don't see an explicit import such as `use App\Http\Controllers\Controller;`. – Matthew Jan 26 '22 at 01:14

1 Answers1

0

As I can see from your code you are using api.php file in order to put your middleware. There, try to change the code as shown :

Route::apiResource('/authors', App\Http\Controllers\AuthorsController::class);

Also, take a look at app\Providers\RouteServiceProvider.php file in order to be sure how it is structured.

One other tip would be to see how Route::ApiResource is structured. It is structured like this: apiResource(string $name, string $controller, array $options = []).

Useful readings apiResource method

Dharman
  • 30,962
  • 25
  • 85
  • 135
Tinxuanna
  • 206
  • 1
  • 3
  • 16
  • 1
    Thanks for response, I change the api and send request again, but getting the same error again and now, I added the app\Providers\RouteServiceProvider.php as well, can you please help me? – krithupayal Jan 21 '22 at 11:51
  • Please try to uncomment this line of code in your RouteServiceProvider.php file "// protected $namespace = 'App\\Http\\Controllers';". – Tinxuanna Jan 21 '22 at 11:58
  • Also is very helpful to visit this answer in [stackoverflow](https://stackoverflow.com/questions/63807930/target-class-controller-does-not-exist-laravel-8) – Tinxuanna Jan 21 '22 at 12:01
  • Still getting someother error – krithupayal Jan 21 '22 at 12:29
  • What kind of error it is? If it is possible try to share. – Tinxuanna Jan 21 '22 at 12:31
  • Illuminate\Contracts\Container\BindingResolutionException: Target class [App\Http\Controllers\App\Http\Controllers\AuthorsController] does not exist. in file C:\Users\kruthy\Desktop\projecrrepository\livechat\laravelapi\laravelbookstoreapi\bookstoreapi\bookstore\vendor\laravel\framework\src\Illuminate\Container\Container.php on line 879 . Stack trace: #0 {main} thrown in C:\Users\kruthy\Desktop\projecrrepository\livechat\laravelapi\laravelbookstoreapi\bookstoreapi\bookstore\routes\api.php on line 21 – krithupayal Jan 21 '22 at 12:34
  • As I can assume from the error message there is 2 times _App\Http\Controllers_\App\Http\Controllers\AuthorsController. Try to avoid this by deleting it from this line of code "Route::apiResource('/authors', _App\Http\Controllers\_AuthorsController::class);" – Tinxuanna Jan 21 '22 at 12:37
  • In api i have to delete? – krithupayal Jan 21 '22 at 12:40
  • Yes, in api.php file. Hope, this helps. If don't we have to try something else. – Tinxuanna Jan 21 '22 at 12:43
  • I dont think Api.php -> Route::middleware('auth:api')->prefix('v1')->group(function() { Route::get('/user', function(Request $request){ return $request->user(); }); Route::apiResource('/authors', App\Http\Controllers\AuthorsController::class); }); – krithupayal Jan 21 '22 at 12:45
  • api.php:21 Stack trace: #0 {main} thrown in C:\Users\kruthy\Desktop\projecrrepository\livechat\laravelapi\laravelbookstoreapi\bookstoreapi\bookstore\routes\api.php on line 21 – krithupayal Jan 21 '22 at 12:47
  • Leave the code in this line like this: **Route::apiResource('/authors', AuthorsController::class); });** in api.php file. I am sorry if we are still stuck. Please, anyone else can help too... – Tinxuanna Jan 21 '22 at 12:54
  • Can you please tell me why do we need to uncomment this line protected $namespace = 'App\\Http\\Controllers';" – krithupayal Jan 21 '22 at 12:56
  • Please visit this [link](https://stackoverflow.com/questions/64037500/defining-a-namespace-for-laravel-8-routes) also in stackoverflow. – Tinxuanna Jan 21 '22 at 13:03
  • same code for me as well, but not finding out where it is, If you are okay. Can you look over my github please? – krithupayal Jan 21 '22 at 13:11
  • Goodmorning! Yes, of course. Please let me know your GitHub account. – Tinxuanna Jan 24 '22 at 07:35
  • @krithupayal Any updates with the error message? I can see from the post that the problem has not been solved. – Tinxuanna Jan 27 '22 at 08:53