【问题标题】:Fatal error: Uncaught Error: Class "App\Http\Controllers\Controller" not found in致命错误:未捕获的错误:找不到类“App\Http\Controllers\Controller”
【发布时间】: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 天吗:(

标签: php laravel-8


【解决方案1】:

从您的代码中可以看出,您正在使用 api.php 文件来放置中间件。在那里,尝试更改代码,如下所示:

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

另外,请查看app\Providers\RouteServiceProvider.php 文件以确定其结构。

另一个提示是查看Route::ApiResource 的结构。它的结构如下:apiResource(string $name, string $controller, array $options = [])

有用的读物​​apiResource method

【讨论】:

  • 感谢您的回复,我更改了api并再次发送请求,但再次出现相同的错误,现在我也添加了app\Providers\RouteServiceProvider.php,您能帮帮我吗?
  • 请尝试在您的 RouteServiceProvider.php 文件 "// protected $namespace = 'App\\Http\\Controllers';" 中取消注释这行代码。
  • stackoverflow访问这个答案也很有帮助
  • 仍然出现其他错误
  • 这是什么错误?如果可能,请尝试分享。
猜你喜欢
  • 1970-01-01
  • 2017-06-09
  • 2018-05-17
  • 2019-07-15
  • 2022-10-14
  • 1970-01-01
  • 1970-01-01
  • 2016-09-09
  • 2023-03-25
相关资源
最近更新 更多