Lae/app/Http/Requests/BaseRequest.php

31 lines
745 B
PHP
Raw Normal View History

2023-01-18 09:36:33 +00:00
<?php
namespace App\Http\Requests;
2023-01-30 16:14:07 +00:00
2023-01-18 09:36:33 +00:00
use Illuminate\Http\Request as HttpRequest;
use Illuminate\Support\Str;
use Symfony\Component\HttpFoundation\ParameterBag;
class BaseRequest extends HttpRequest
{
// 根据实际情况调整长度
const JSON_MAX_LENGTH = 65535;
public function json($key = null, $default = null)
{
2023-02-07 09:04:11 +00:00
if (! isset($this->json)) {
2023-01-18 09:36:33 +00:00
$content = $this->getContent();
2023-02-07 09:04:11 +00:00
$parameters = Str::length($content) > static::JSON_MAX_LENGTH ? [] : (array) json_decode($content, true);
2023-01-18 09:36:33 +00:00
$this->json = new ParameterBag($parameters);
}
if (is_null($key)) {
return $this->json;
}
return data_get($this->json->all(), $key, $default);
}
}