lrezek / cbor4laravel
为 Laravel 提供的 CBOR 服务。
Requires
- php: >=5.3.0
- illuminate/http: >=4.1.0
- illuminate/support: >=4.1.0
- lemonblast/cbor4php: dev-master
This package is not auto-updated.
Last update: 2024-09-24 01:56:23 UTC
README
#关于 Cbor4Laravel 为 Laravel 请求和响应添加 CBOR 功能。有关 CBOR 的更多信息,请参阅 规范。
#安装
将 lrezek/cbor4laravel
添加到 composer.json
的依赖中
{ "require": { "lrezek/cbor4laravel": "dev-master" } }
根据您的稳定性设置,您可能还需要添加对 "lemonblast/cbor4php": "dev-master"
的依赖。
使用 composer update
更新您的包或使用 composer install
安装。
##请求 由于请求类在 Laravel 生命周期中创建得较早,您需要覆盖 bootstrap/start.php
中的实现。在文件开头添加以下内容
use Illuminate\Foundation\Application; Application::requestClass('LRezek\Cbor4Laravel\Http\Request');
这启用了以下 3 个方法
Request::isCbor(); //Returns true if the request is in CBOR format, false otherwise. Request::wantsCbor(); //Returns true if the accept type of the request is CBOR, false otherwise. Request::cbor(); //Returns the decoded request content.
请注意,这三个方法在 Input
Facade 中也可用,因为 Input
和 Request
在 Laravel 中是同一个对象。
与 Input::json()
类似,Input::cbor()
允许您以 ParameterBag
的形式获取 CBOR 解码的数据。这意味着您可以指定一个键和一个默认值来获取,或者获取所有输入作为一个数组
Input::cbor()->all(); //Get all input as an array. Input::cbor($key); //Get value of the specified key in the input. Input::cbor($key, $default); //Get value of specified key in the input, or the specified $default if the key isn't found.
##响应 要启用 CBOR 响应,只需将 Laravel 的 Response
Facade 替换为 app/config/app.php
中 aliases
数组的 Response
。
//'Response' => 'Illuminate\Support\Facades\Response', 'Response' => 'LRezek\Cbor4Laravel\Facades\Response',
这允许您像使用 Response::json()
一样使用 Response::cbor()
。有关此示例,请参阅 Laravel 文档。
此外,您还可以创建一个自定义响应并将其格式化为 CBOR,如下所示
Response::make($content)->format('cbor'); //Returns a CBOR formatted Response.