bright-components / common
BrightComponents 包的通用组件。
Requires
- php: ^7.1.3
- illuminate/support: 5.7.* || 5.8.*
Requires (Dev)
- phpunit/phpunit: ^7.0
This package is auto-updated.
Last update: 2019-03-31 20:46:52 UTC
README
BrightComponents 包的通用组件。
免责声明
BrightComponents 命名空间下的包基本上是我避免在我所有项目中重复粘贴我喜欢的基本功能的一种方式。这里没有什么突破性的东西,只是为表单请求、控制器、自定义规则、服务等提供一些额外的功能。
该包包含可能在其他 BrightComponents 包中常见的类、接口等。
安装
您可以通过 composer 安装此包
composer require bright-components/common
Laravel 版本 > 5.6.0 将自动识别并注册服务提供者。如果您已禁用此功能,请将包服务提供者添加到您的 config/app.php 文件中的 'providers' 数组中
'providers' => [ //... BrightComponents\Common\BrightComponentsServiceProvider::class, //... ];
使用方法
目前,'common' 包仅提供 Payload 类和一些 ResponseFactory 宏,以帮助将 Payload 转换为 json 响应或返回带有 payload 数据的视图。以下提供的示例使用控制器类中的 Payload。
public function index(Request $request) { $users = \App\Models\User::get(); // As an alternative to "new Payload", you could resolve an instance via your controller's constructor. $payload = (new Payload)->setOutput($users) ->setMessages(['success' => 'Operation successful!']) ->setStatus($payload::STATUS_OK); return response()->jsonWithPayload($payload); // return response()->viewWithPayload('dashboard', $payload, 'users'); }
消息和状态是可选的。您可以在返回的响应中使用这些值,方法自便。
若要可选地使用键包裹输出,请将键(字符串)作为 setOutput
的第二个参数传递
$payload->setOutput($users, 'data');
如果您的 payload 包含 'messages',输出将自动用键包裹。如果您不提供包装键,将使用键 'data'。
响应助手
如上所示,提供了一些 ResponseFactory 宏,使发送 payload 响应更加容易。例如
$payload->setOutput($users, 'users')->setMessages(['success' => 'Operation Successful!']); response()->jsonWithPayload($payload);
将产生以下结构
{
"users": [
{
"id": 1,
"name": "Clayton Stone",
"email": "clay@test.com",
"email_verified_at": "2019-03-18 20:29:26",
"created_at": "2019-03-18 20:29:26",
"updated_at": "2019-03-18 20:29:26"
},
{
"id": 2,
"name": "John Doe",
"email": "john15@gmail.com",
"email_verified_at": "2019-03-23 18:20:11",
"created_at": "2019-03-23 18:19:41",
"updated_at": "2019-03-23 18:20:16"
}
],
"messages": {
"success": "Operation successful!"
}
}
其他助手是 viewWithPayload()
response()->viewWithPayload('dashboard', $payload, 'payload');
第三个参数是您将在视图中使用的字符串。默认情况下,使用 'payload'。使用以下
$payload->setOutput($users, 'users')->setMessages(['success' => 'Operation Successful!']); return response()->viewWithPayload('dashboard', $payload);
您可以这样访问您的数据
<h1>{{ $payload->name }}</h1> <span>{{ $payload->email }}</span>
但是,您可以以下方式设置变量名称
$payload->setOutput($users, 'users')->setMessages(['success' => 'Operation Successful!']); return response()->viewWithPayload('dashboard', $payload, 'user');
并按以下方式访问您的数据
<h1>{{ $user->name }}</h1> <span>{{ $user->email }}</span>
如果您选择不使用助手方法,请参考下面的 PayloadContract 以获取 Payload 实例上的可用方法
<?php namespace BrightComponents\Common\Payloads\Contracts; interface PayloadContract extends Status { /** * Set the Payload status. * * @param string $status * * @return $this */ public function setStatus($status); /** * Get the status of the payload. * * @return string */ public function getStatus(); /** * Set the Payload output. * * @param mixed $output * @param string|null $wrapper * * @return $this */ public function setOutput($output, ? string $wrapper = null); /** * Get the Payload output. * * @return array */ public function getOutput(); /** * Get the unwrapped Payload output. * * @return array */ public function getUnwrappedOutput(); /** * Set the Payload messages. * * @param array $output * * @return $this */ public function setMessages(array $messages); /** * Get messages array from the payload. * * @return array */ public function getMessages(); /** * Get the wrapper for the output. * * @return string */ public function getOutputWrapper(); /** * Get the wrapper for messages. * * @return string */ public function getMessagesWrapper(); }
测试
composer test
变更日志
请参阅 CHANGELOG 了解最近更改的更多信息。
贡献
请参阅 CONTRIBUTING 了解详情。
安全
如果您发现任何与安全相关的问题,请发送电子邮件至 clay@phpstage.com,而不是使用问题跟踪器。
路线图
我们计划尽快着手处理灵活性/配置问题,以及发布一个与框架无关的包版本。
致谢
许可
MIT 许可证 (MIT)。请参阅 许可文件 获取更多信息。