Laravel 8升级后出现BindingResolutionException错误

时间:2020-10-02 19:12:49

标签: php laravel laravel-8

Laravel v:7升级到8后,我遇到错误,但无法解决

Illuminate\Contracts\Container\BindingResolutionException
Target class [Frontend\HomeController] does not exist. 

我的控制器namespace和目录结构:

|- app
    |- Http
        |- Controllers
            |- Frontend
                 -HomeController.php
             - Controller.php

路线:

Route::namespace('Frontend')->group(function () {
    Route::get('/', 'HomeController@index')->name('home');
});

然后是HomeController.php

<?php

namespace App\Http\Controllers\Frontend;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class HomeController extends Controller
{
    public function index()
    {
        dd('aaaa');
    }

    public function create()
    {
        //
    }

    public function store(Request $request)
    {
        //
    }

    public function show($id)
    {
        //
    }

    public function edit($id)
    {
        //
    }

    public function update(Request $request, $id)
    {
        //
    }

    public function destroy($id)
    {
        //
    }
}

使用以下命令也无法解决我的问题:

php artisan clear-compiled
php artisan optimize:clear
composer dump-autoload

1 个答案:

答案 0 :(得分:2)

在laravel 8中,您需要这样做:

use App\Http\Controllers\Frontend\HomeController;

Route::get('/', [HomeController::class, 'index'])->name('home');

V8中的Laravel不会自动命名空间前缀。