【问题标题】:Lumen Illuminate Http Client Request unresolvable dependencyLumen Illuminate Http 客户端请求无法解析的依赖
【发布时间】:2021-04-10 07:12:48
【问题描述】:

我正在尝试使用 Laravel\Lumen 和 Eloquent 通过 POST 方法存储数据。 我使用的数据库是 MySQL。

这是我的 CategoriesController.php:

<?php

namespace App\Http\Controllers;

use App\Category;
use Illuminate\Http\Client\Request;

class CategoriesController extends Controller
{
    public function index()
    {
        return Category::all();
    }

    public function create(Request $request)
    {
        return response()
            ->json(
                Category::create(['description' => $request->description]),
                201
            );
    }
}

这是我的 CategoryModel.php:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Category extends Model
{
    protected $fillable = ['description'];

    public function expense()
    {
        return $this->belongsTo(Expense::class);
    }
}

在我的 App.php 文件中,我取消了注释:

$app->withFacades();
$app->withEloquent();

我的 Web.php 文件:

$router->group(['prefix' => 'api'], function ($router){
   

    $router->get('categories', 'CategoriesController@index');
    $router->post('categories', 'CategoriesController@create');
});

我通过 Postman 发送以下发帖请求:

{
        "description" : "teste"
}

发送请求后,我收到了 500 HTTP 状态响应。 该错误包含以下 StackTrace:

BindingResolutionException HTTP 500 内部服务器错误

in C:\Fontes\PHP\budget-manager\vendor\illuminate\container\Container.php (line 1053)
     */    protected function unresolvablePrimitive(ReflectionParameter $parameter)
        {
            $message = "Unresolvable dependency resolving [$parameter] in class {$parameter->getDeclaringClass()->getName()}";
            throw new BindingResolutionException($message);
        }

【问题讨论】:

  • 你如何在控制器中调用?
  • 我现在才发现是什么导致了这个问题。请注意,在 CategoriesController 文件中,我使用的是use Illuminate\Http\Client\Request;。我已将其转换为 use Illuminate\Http\Request;,在此更改之后,我可以毫无问题地发送请求

标签: php mysql laravel post lumen


【解决方案1】:

我发现了问题。

我的 VSCode 正在自动导入 use Illuminate\Http\Client\Request 依赖项。

在努力找出问题所在后,我发现正确的依赖是use Illuminate\Http\Request;

希望此解决方案适用于遇到同样问题的人。

【讨论】:

    猜你喜欢
    • 2021-06-12
    • 2019-04-23
    • 2017-12-25
    • 2019-05-25
    • 2023-02-03
    • 2010-09-12
    • 1970-01-01
    • 2019-08-01
    • 2016-10-24
    相关资源
    最近更新 更多