mediadevs/response-handler
为我的项目提供的简单响应处理程序,易于修改。
1.0.0
2019-11-04 10:56 UTC
Requires
- php: ^7.1
- ext-json: *
Requires (Dev)
This package is auto-updated.
Last update: 2024-09-13 03:39:46 UTC
README
请注意!
并非所有内容都已测试,请自行承担风险使用。
安装
通过Composer
$ composer require mediadevs/response-handler
通过GIT
HTTPS: git clone https://github.com/mediadevs/response-handler.git SSH: git clone git@github.com:mediadevs/response-handler.git
用法
基本消息
<?php use mikevandiepen\utility\Response; // Instantiating the Response class $response = new Response(); // Adding messages and returning a json response $response->add('Action executed successfully')->toJSON(); ?>
响应
[ {"level":"primary","message":"Action executed successfully"} ]
单个消息
<?php use mikevandiepen\utility\Response; // Instantiating the Response class $response = new Response(); // Adding messages $response->add('Action executed successfully', [ /** There must be an array, but can be left empty*/ ], Response::SUCCESS); // Returning the message in json response $response->toJSON(); ?>
响应
[ {"level":"success","message":"Action executed successfully"} ]
多个参数
<?php use mikevandiepen\utility\Response; // Instantiating the Response class $response = new Response(); // Adding messages $response->add('{%action%} executed {%status%}', ['action' => 'My Custom Action', 'status' => 'successfully'], Response::SUCCESS); // Returning the message in json response $response->toJSON(); ?>
响应
[ {"level":"success","message":"My Custom Action executed successfully"} ]
带有参数的多个消息
<?php use mikevandiepen\utility\Response; // Instantiating the Response class $response = new Response(); // Adding messages $response->add('{%action%} executed successfully', ['action' => 'your_action'], Response::SUCCESS); $response->add('DUPLICATE {%action%}!', ['action' => 'your_action_2'], Response::WARNING); $response->add('DUPLICATE {%action%}!', ['action' => 'your_action_3'], Response::WARNING); $response->add('{%action%} does not exist', ['action' => 'your_action_4'], Response::ERROR); // Returning the messages in json response $response->toJSON(); ?>
响应
[ {"level":"success","message":"your_action executed successfully"}, {"level":"warning","message":"DUPLICATE your_action_2!"}, {"level":"warning","message":"DUPLICATE your_action_3!"}, {"level":"danger","message":"your_action_4 does not exist"} ]
高亮参数
<?php use mikevandiepen\utility\Response; // Instantiating the Response class $response = new Response(); // Adding messages $response->add('Look how cool this is! {%parameter%}', ['parameter' => 'Highlight me!'], Response::SUCCESS)->delimiters('"'); // Returning the message in json response $response->toJSON(); ?>
响应
[ {"level":"success","message":"Look how cool this is! \"Highlight me!\""} ]
在这种情况下,我们只使用了单个封装字符串,应用于两侧。如果您的案例需要在两侧使用不同的字符,您可以在数组中添加第二个元素。
例如
<?php use mikevandiepen\utility\Response; // Instantiating the Response class $response = new Response(); // Adding messages $response->add('Look how cool this is! {%parameter%}', ['parameter' => 'Highlight me!'], Response::SUCCESS)->delimiters('<strong>', '</strong>'); // Returning the message in json response $response->toJSON(); ?>
响应
[ {"level":"success","message":"Look how cool this is! <strong>Highlight me!</strong>"} ]
以数组形式返回响应
<?php use mikevandiepen\utility\Response; // Instantiating the Response class $response = new Response(); // Adding messages $response->add('Look how cool this is! {%parameter%}', ['parameter' => 'Highlight me!'], Response::SUCCESS)->delimiters('"'); // Returning the message in json response $response->toArray(); ?>
响应
array( "level" => "success", "message" => "Look how cool this is! \"Highlight me!\"" );
## Change log
Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.
## Testing
``` bash
$ composer test
贡献
请参阅CONTRIBUTING和CODE_OF_CONDUCT以获取详细信息。
安全
如果您发现任何与安全相关的问题,请通过电子邮件contact@mediadevs.nl联系,而不是使用问题跟踪器。
致谢
许可
MIT许可(MIT)。有关更多信息,请参阅许可文件。