sirmekus/zam 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

sirmekus/zam

Composer 安装命令:

composer require sirmekus/zam

包简介

A PHP package for handling client requests, sending response, among others, easily

README 文档

README

Zam helps to receive and process client request(s), return response to client with appropriate header for a more robust and easier development

Please note that database operations have been moved to its own special repository/package. If you need support for database operation use Ahia instead.

For receiving request from client you will likely do something like:

if(isset($_POST['email']) and isset($_POST['name']) and isset($_POST['password'])){
    //take input
}
else{
    // cancel operation or return warning
}

With this package you can simply do:

require_once 'path_to_vendor/autoload.php';

$name = request(["name"=>"name");

$email = request(["name"=>"email");

$password = request(["name"=>"password");

//continue execution

Installation

To get started all you need to do is:

composer require sirmekus/zam

and you're in. Please note that if you use Laravel framework then this package may clash with it because there exists functions with similar names as in Laravel. Zam, in Igbo language, means "Answer Me" and it does this exactly. If you use Laravel then you may not need this package.

Usage

Receiving/Accepting Request(s)

To accept request you just need to pass an optional configuration array to the request() function as key-value pairs. Only one key is important to be passed - the NAME key. E.g:

require_once 'path_to_vendor/autoload.php';

$name = request(["name"=>"name"]);

$email = request(["name"=>"email", "message"=>"Please provide your email address"]);

$password = request(["name"=>"password", "method"=>"post"]);

The possible configuration keys are:

  • name : This is the name of the input/request coming from your front end.
  • required : Boolean value that indicates whether the expected input must be present. Default is set to true.
  • method : The expected method (HTTP verbs: GET, POST, PUT, etc) that the expected input must follow. Default is POST
  • message : If present this will be sent back to the client if the name is not set or empty.
  • nullable : If an expected input isn't set or is empty it tells us whether to proceed with request or throw error to client.

Another way to receive input from client is by calling the request() function without any argument then access the expected input as a dynamic property on it. Example:

require_once 'path_to_vendor/autoload.php';

request()->name; 

In the above example, if name is set in the form it'll return the value else it returns null.

Inputs are sanitized before being passed to your application. Note that this function can also sanitize arrays when passed to it. Validation should be done on client side including error checks.

Also, this package integrates well with Zam package when using AJAX for making request(s) from front end.

Note that you can accept any request with any HTTP verb. However, for requests that don't use the GET, POST or PUT method we encourage you to pass the request in "REQUEST PAYLOAD" (JSON) format (instead of Formdata). Also, to verify that the request uses a particular HTTP verb you should check the $_SERVER['REQUEST_METHOD']

Response(s)

This will typically be useful to users who use axios library or Zam package as the appropriate HTTP status header will be specified. E.g

require_once 'path_to_vendor/autoload.php';

return response("Thank you for using Zam.");

You can pass an optional parameter as second argument to this function which is the HTTP status code to send to the client. By default a 200 HTTP status code is sent to the client. Example:

require_once 'path_to_vendor/autoload.php';

return response("There was an error in submission", 403);

Note that you can also pass an array as argument to this function and it'll be converted to JSON before being sent to client.

The supported HTTP status code (and their meanings) you can pass and that can be sent to client are:

  • 200=>ok,
  • 201=>Created,
  • 202=>Accepted,
  • 204=>No Content,
  • 301=>Moved Permanently,
  • 308=>Permanent Redirect,
  • 422=>Unprocessable Entity,
  • 401=>unauthorized,
  • 403=>forbidden,
  • 404=>Not Found,
  • 405=>Method Not Allowed,
  • 500=>Internal Server Error,
  • 503=Service Unavailable,
  • 408=>Request Timeout,
  • 411=>Length Required,
  • 413=>Payload Too Large,
  • 406=>Not Acceptable

Only the code is needed to be passed.

Displaying Response to user(s)

This package exposes a function called error() which accepts the HTML field's name as argument and display any error related to that field as set by the server. E.g:

//index.php
<?php
require_once 'vendor/autoload.php';
?>

<!DOCTYPE html>
<html lang="en">

...

<body>
    <form>
    ...
    <div>
    <label>Name</label>
    <input type="text" name="name" />
    <?php if(error('name')) echo error('name'); ?>
    </div>
    ...
    </form>

</body>

</html>

Now if there's an error for the name field above it will be displayed accordingly. You can do this for other fields as well by supplying the appropriate name.

Meanwhile

You can connect with me on LinkedIn for insightful tips and so we can grow our networks together.

Check our educational platform for High Schools i-runs.

And follow me on Twitter.

I encourage contribution even if it's in the documentation. Thank you, and I really hope you find this package helpful.

sirmekus/zam 适用场景与选型建议

sirmekus/zam 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 52 次下载、GitHub Stars 达 3, 最近一次更新时间为 2022 年 08 月 23 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 sirmekus/zam 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 sirmekus/zam 我们能提供哪些服务?
定制开发 / 二次开发

基于 sirmekus/zam 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 52
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 8
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

  • Stars: 3
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-08-23