Reputation: 21
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.
Upvotes: 1
Views: 9240
Reputation: 230
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
Upvotes: 0