目标[Illuminate \ Contracts \ Routing \ ResponseFactory]不可实例化

时间:2016-12-21 15:47:34

标签: lumen

我想回复这样的回复:

return response()->json(['name' => 'Abigail', 'state' => 'CA']);

然而,我收到了错误:

Target [Illuminate\Contracts\Routing\ResponseFactory] is not instantiable.

有什么想法吗?

这是我的composer.json:

{
  "name": "laravel/lumen",
  "description": "The Laravel Lumen Framework.",
  "keywords": [
    "framework",
    "laravel",
    "lumen"
  ],
  "license": "MIT",
  "type": "project",
  "require": {
    "php": ">=5.5.9",
    "laravel/lumen-framework": "5.2.*",
    "vlucas/phpdotenv": "~2.2",
    "generationtux/jwt-artisan": "^0.1.7",
    "barryvdh/laravel-cors": "^0.8.0",
    "neomerx/cors-illuminate": "^1.1",
    "fenos/notifynder": "3.1.*",
    "franzose/closure-table": "^4.1",
    "mlntn/lumen-artisan-serve": "~1",
    "guzzlehttp/guzzle": "~6.0",
    "league/flysystem": " ~1.0",
    "bugsnag/bugsnag-laravel": "^2.0"
  },
  "require-dev": {
    "fzaninotto/faker": "~1.4",
    "phpunit/phpunit": "~4.0"
  },
  "autoload": {
    "psr-4": {
      "App\\": "app/",
      "GuzzleHttp\\": "/vendor/guzzlehttp/"
    },
    "classmap": [
      "database/"
    ]
  },
  "autoload-dev": {
    "classmap": [
      "tests/",
      "database/"
    ]
  },
  "config": {
    "preferred-install": "dist"
  }
}

5 个答案:

答案 0 :(得分:6)

在2020年我仍然收到错误消息。这是@sunben解决方案的更新版本:

bootstrap / app.php 中,取消注释以下行

$app->register(App\Providers\AppServiceProvider::class);

然后在 app \ Providers \ AppServiceProvider.php 中,更新注册方法以添加:

$this->app->singleton(\Illuminate\Contracts\Routing\ResponseFactory::class, function() {
    return new \Laravel\Lumen\Http\ResponseFactory();
});

答案 1 :(得分:1)

可能会迟到但找到解决方案。

bootstrap / app.php 中,取消注释以下行

$app->register(App\Providers\AppServiceProvider::class);

然后在 app \ Providers \ AppServiceProvider.php 中,更新寄存器方法添加:

public function register()
{
    $this->app->singleton('Illuminate\Contracts\Routing\ResponseFactory', function ($app) {
        return new \Illuminate\Routing\ResponseFactory(
            $app['Illuminate\Contracts\View\Factory'],
            $app['Illuminate\Routing\Redirector']
        );
    });
}

答案 2 :(得分:1)

试试这个 public function register() { $this->app->singleton(\Illuminate\Contracts\Routing\ResponseFactory::class, function() { return new \Laravel\Lumen\Http\ResponseFactory(); }); $this->app->bind(\Illuminate\Contracts\Routing\UrlGenerator::class, function ($app) { return new \Laravel\Lumen\Routing\UrlGenerator($app); }); }

答案 3 :(得分:0)

那些仍然遇到路由相关错误的人,只需将以下行添加到composer.json文件中并运行命令composer update

"illuminate/routing": "^5.6"

composer update
我们在这里热身,祝所有事情现在都很好用

答案 4 :(得分:0)

response帮助器可用于方便地生成其他类型的响应实例。调用response帮助器时不带参数时,将返回Laravel \ Lumen \ Http \ ResponseFactory类的实现。

use Laravel\Lumen\Http\ResponseFactory;