dqnek0079 2019-08-08 16:57
浏览 124

如何使用Laravel JSON API?

I'm trying to use {JSON:API} with my Laravel app, and I can retrieve a list of records, but cannot access a single record.

I strictly followed the JSON:API tutorial (https://howtojsonapi.com/laravel.html), and I managed to run it and get the index list.

I used artisan to create all needed files : Adapter, Schema and Validator.

json-api-v1.php :

'resources' => [ 'titre' => App\Models\TitreModel::class, ];

api.php :

JsonApi::register('v1')->routes(function ($api, $router) {
        $api->resource('titre');
});

TitreSchema.php :

class TitreSchema extends SchemaProvider
{
    protected $resourceType = 'titre';

    public function getId($resource)
    {
    }

    public function getAttributes($resource)
    {
        return [
            'ID'      => $resource->ID,
            'ID_DICO' => $resource->ID_DICO,
        ];

TitreAdapter.php :

class TitreAdapter extends AbstractAdapter
{
    protected $attributes = [
        'ID'      => 'ID',
        'ID_DICO' => 'ID_DICO',
    ];

    protected $relationships = [];

    protected $primaryKey = 'ID';

    public function __construct(StandardStrategy $paging)
    {
        parent::__construct(new TitreModel(), $paging);
    }

TitreController.php :

class TitreController extends BaseController
{
    public function index()
    {
        return new TitreCollection(TitreModel::paginate(parent::handlePerPage()));
    }

    public function show(TitreModel $titre)
    {   
        return new TitreResource($titre);
    }

When I visit /api/v1/titre, it's ok, I can get the json:api formatted records list.
But when I visit /api/v1/titre/1, I get this error :

"messages":{"count":1,"messages":[{"message":"[15:37:22] LOG.error: Argument 1 passed to Illuminate\\Routing\\Middleware\\ThrottleRequests::addHeaders() must be an instance of Symfony\\Component\\HttpFoundation\\Response, null given, called in C:\\www\\AppFlx\\dev\\dk_api\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Middleware\\ThrottleRequests.php on line 62

I'm the first to use JSON:API in my enterprise, so none of my colleagues can help me.
I only followed the tutorial, added nothing, changed nothing, and I don't use headers, so I don't get where it comes from.

  • 写回答

1条回答 默认 最新

  • douchen7324 2019-08-09 08:56
    关注

    I found where it came from.

    The problem came from App\Exceptions\Handler.php, with the render method.
    Cause I made a simple copy-paste from the tutorial, and didn't check it.
    The tutorial not-working Handler file :

    public function render($request, Exception $e)
    {
        if ($this->isJsonApi($request, $e)) {
          return $this->renderJsonApi($request, $e);
        }
    
        // do standard exception rendering here...
    }
    

    Problem was I didn't add a return for standard exceptions.

    The modified working Handler file :

    public function render($request, Exception $e)
      {
        return $this->renderJsonApi($request, $e);
      }
    

    Works, since the method always have a return.

    评论

报告相同问题?

悬赏问题

  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程