PHPackages                             sirmekus/zam - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [HTTP &amp; Networking](/categories/http)
4. /
5. sirmekus/zam

ActiveLibrary[HTTP &amp; Networking](/categories/http)

sirmekus/zam
============

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

2.0.5(2y ago)3522MITPHPPHP &gt;=5.4.0

Since Aug 23Pushed 2y ago1 watchersCompare

[ Source](https://github.com/SirMekus/zam)[ Packagist](https://packagist.org/packages/sirmekus/zam)[ RSS](/packages/sirmekus-zam/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (1)Versions (17)Used By (2)

Zam
===

[](#zam)

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](https://github.com/SirMekus/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
------------

[](#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
-----

[](#usage)

---

Receiving/Accepting Request(s)
------------------------------

[](#receivingaccepting-requests)

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](https://www.npmjs.com/package/mmuo) 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)
-----------

[](#responses)

This will typically be useful to users who use `axios` library or [Zam](https://www.npmjs.com/package/mmuo) 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=&gt;ok,
- 201=&gt;Created,
- 202=&gt;Accepted,
- 204=&gt;No Content,
- 301=&gt;Moved Permanently,
- 308=&gt;Permanent Redirect,
- 422=&gt;Unprocessable Entity,
- 401=&gt;unauthorized,
- 403=&gt;forbidden,
- 404=&gt;Not Found,
- 405=&gt;Method Not Allowed,
- 500=&gt;Internal Server Error,
- 503=Service Unavailable,
- 408=&gt;Request Timeout,
- 411=&gt;Length Required,
- 413=&gt;Payload Too Large,
- 406=&gt;Not Acceptable

**Only the code is needed to be passed.**

---

Displaying Response to user(s)
------------------------------

[](#displaying-response-to-users)

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

...

    ...

    Name
