【发布时间】:2022-01-23 19:05:58
【问题描述】:
致命错误:未捕获的错误:在 C:\Users\krithu\livechat\laravelapi\laravelbookstoreapi\bookstoreapi\bookstore\app\Http\Controllers\AuthorsController.php:10 中找不到类“App\Http\Controllers\Controller” 堆栈跟踪: #0 {主要} 在第 10 行的 C:\Users\krithu\projecrrepository\laravelapi\laravelbookstoreapi\bookstoreapi\bookstore\app\Http\Controllers\AuthorsController.php 中抛出
下面是我的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;
}
下面是我的 api.php
Route::middleware('auth:api')->prefix('v1')->group(function() {
Route::get('/user', function(Request $request){
return $request->user();
});
Route::apiResource('/authors', AuthorsController::class);
});
下面是我的 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)
{
//
}
}
下面是我的 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());
});
}
}
我正在发帖请求http://127.0.0.1:8000/api/v1/authors 根据路由列表,它应该执行 store 方法并返回 Test 的输出。
【问题讨论】:
-
fxi,错误出现在
AuthorsController.php -
@brombeer-> 我现在更新了。你能帮帮我吗?
-
“下面是我的
Controllers.php”……你的意思是Controller.php,对吧?因为这个类被命名为Controller而不是Controllers -
感谢回复,Controller.php 仅在我的 vscode 中。在将 false 设置为 true 以进行身份验证后,我的错误现在发生了一些变化。TypeError: Illuminate\Validation\Factory::make(): Argument #2 ($rules) must be of type array, App\Http\Resources\AuthorsResource given,在 projecrrepository\livechat\laravelapi\laravelbookstoreapi\bookstoreapi\bookstore\vendor\laravel\framework\src\Illuminate\Foundation\Http\FormRequest.php 文件 laravelbookstoreapi\bookstoreapi\bookstore\vendor\laravel\framework\src\ 中的第 114 行调用Illuminate\Validation\Factory.php 在第 105 行
-
stackover 中的某个人可以帮我在这里等待 2 天吗:(