php中response失败,Lumen 报错提示 实例不了 Response 类的问题

今天使用Lumen的时候,用到了Response类,很奇怪提示:Target [Illuminate\Contracts\Routing\ResponseFactory] is not instantiable.

大概就是说实例不了Response 类,那怎么解决呢?我们以一个全新的Lumen项目来说

1.我在web.php写了个路由

/*

|--------------------------------------------------------------------------

| Application Routes

|--------------------------------------------------------------------------

|

| Here is where you can register all of the routes for an application.

| It is a breeze. Simply tell Lumen the URIs it should respond to

| and give it the Closure to call when that URI is requested.

|

*/

use Illuminate\Support\Facades\Response;

$router->get('/', function () use ($router) {

return Response::json('123456',200);

});

然后访问这个路由报错如下(也就是我们要解决的错误):

a8689384ff4bd33a6b029b637e71f708.png

2.解决办法

2.1 打开项目根目录下的 bootstrap/app.php

//找到这两行把注释去掉

$app->withFacades();

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

2.2 找到 项目根目录下的 app/Providers/AppServiceProvider.php

namespace App\Providers;

use Illuminate\Routing\ResponseFactory;

use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider

{

/**

* Register any application services.

*

* @return void

*/

public function register()

{

}

}

在 register 注册 ResponseFactory 修改如下:

namespace App\Providers;

use Illuminate\Routing\ResponseFactory;

use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider

{

/**

* Register any application services.

*

* @return void

*/

public function register()

{

$this->app->singleton('Illuminate\Contracts\Routing\ResponseFactory', function ($app)

{

return new ResponseFactory(

$app['Illuminate\Contracts\View\Factory'],

$app['Illuminate\Routing\Redirector']);

});

}

}

这时候还不行, 我们还需要安装一个 库,切换到项目根目录 执行composer命令:

composer require "illuminate/routing"

库安装完后,我们访问试试:

be45ac099eb2f2ba2b0ba5019ad50e96.png

很简单吧,这样就可以啦~~

特别提示:

本人正在培养自己的写作水平,会把平时遇到的问题和有趣的东西记录下来,如果对你有帮助请动动小手点个赞支持支持,大神莫喷,谢谢!!

本作品采用《CC 协议》,转载必须注明作者和本文链接

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值