Laravel 6.0 php artisan route:list返回“目标类[App \ Http \ Controllers \ SessionsController]不存在。”

And*_*eas 2 laravel artisan

我正在使用Laravel 6.0,尝试使用列出所有路线artisan route:list,但失败并返回:

Illuminate \ Contracts \ Container \ BindingResolutionException:目标类[App \ Http \ Controllers \ SessionsController]不存在。

在/home/vagrant/code/vendor/laravel/framework/src/Illuminate/Container/Container.php:806 802 | 803 | 尝试{804 | $ reflector = new ReflectionClass($ concrete); 805 | } catch(ReflectionException $ e){

806 | 抛出新的BindingResolutionException(“目标类[$ concrete]不存在。”,0,$ e); 807 | } 808 | 809 | //如果类型不可实例化,则开发人员正在尝试解析810 |。//一个抽象类型,例如Interface或Abstract Class

异常跟踪:

1 Illuminate \ Foundation \ Console \ RouteListCommand :: Illuminate \ Foundation \ Console {closure}(Object(Illuminate \ Routing \ Route))[内部]:0

2 ReflectionException::(“ Class App \ Http \ Controllers \ SessionsController不存在”)/home/vagrant/code/vendor/laravel/framework/src/Illuminate/Container/Container.php:804

3 ReflectionClass :: __ construct(“ App \ Http \ Controllers \ SessionsController”)/home/vagrant/code/vendor/laravel/framework/src/Illuminate/Container/Container.php:804

到目前为止,我只有一个非常简单的web.php路由文件:

Route::get('/', function () {
    return view('index');
});


Route::prefix('app')->group(function () {
    // Registration routes
    Route::get('registration/create', 'RegistrationController@create')->name('app-registration-form');
});


// Templates
Route::get('templates/ubold/{any}', 'UboldController@index');
Run Code Online (Sandbox Code Playgroud)

知道如何调试此问题吗?

提前谢谢了!

Ale*_*orb 68

我正在从 Laravel 7 升级到Laravel 8(Laravel 8 仍在开发中)并且也遇到了这个问题。

解决方案是在路由中使用控制器的类名表示:

所以在web.php而不是

Route::get('registration/create', 'RegistrationController@create')
Run Code Online (Sandbox Code Playgroud)

就是现在:

解决方案 1:类名表示

use App\Http\Controllers\RegistrationController;

Route::get('/', [RegistrationController::class, 'create']);
Run Code Online (Sandbox Code Playgroud)

解决方案 2:字符串语法

或作为字符串语法(完整的命名空间控制器名称):

Route::get('/', 'App\Http\Controllers\RegistrationController@create');
Run Code Online (Sandbox Code Playgroud)

解决方案 3:返回之前的行为

由于这应该仅在您通过创建全新的 Laravel 项目升级应用程序时才会发生,您也可以将默认命名空间添加到 RouteServiceProvider

应用程序/Providers/RouteServiceProvider.php

class RouteServiceProvider extends ServiceProvider
{
    /* ... */
     
    /** ADD THIS PROPERTY
     * If specified, this namespace is automatically applied to your controller routes.
     *
     * In addition, it is set as the URL generator's root namespace.
     *
     * @var string
     */
    protected $namespace = 'App\Http\Controllers';

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

        $this->routes(function () {
            Route::middleware('web')
                ->namespace($this->namespace) // <-- ADD THIS
                ->group(base_path('routes/web.php'));

            Route::prefix('api')
                ->middleware('api')
                ->namespace($this->namespace) // <-- ADD THIS
                ->group(base_path('routes/api.php'));
        });
    }

    /* ... /*
}
Run Code Online (Sandbox Code Playgroud)

另请参阅https://laravel.com/docs/8.x/routing#basic-routinghttps://laravel.com/docs/8.x/upgrade(搜索“路由”)。

  • 刚刚遇到这个!希望文档已更新+他们不会改变这样的小事情。恼人的。 (4认同)

Bor*_*s N 38

对于那些对Illuminate\Contracts\Container\BindingResolutionException : Target class [<className>] does not exist.消息有类似问题的人,这也可能会有所帮助:

composer dump-autoload
Run Code Online (Sandbox Code Playgroud)


小智 7

运行此命令

  php artisan config:cache 
Run Code Online (Sandbox Code Playgroud)

  • 不适合我。 (2认同)

Mig*_*ens 6

就我而言,它是通过运行解决的

php artisan optimize:clear
php artisan config:cache
Run Code Online (Sandbox Code Playgroud)

命令optimize:clear清除一切


aph*_*hoe 5

就我而言,这是 Linux 文件名大小写敏感性的问题。对于名为 的文件IndexController,havingIndexcontroller在 Windows 中有效,但在 Linux 中无效


ZBo*_*ala 5

只需添加以下行即可app->Providers->RouteServiceProvider.php

protected $namespace = 'App\\Http\\Controllers';
Run Code Online (Sandbox Code Playgroud)