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

Related tags

Third Party APIs zam
Overview

Zam

Zam helps to receive and process client request(s), return response to client with appropriate header and database CRUD operation for a more robust and syntactic operation.

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.


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 that the expected input must follow (between GET and POST). 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 is 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.


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.


Database


With this feature you don't have to craft (complicated) SQL queries in your script anymore and make your code more readable and clean. Welcome to the OOP way.

Using the database feature requires knowledge of our convention. But first, to start using this feature you should import the Model class to your script like so:

require_once 'path_to_vendor/autoload.php';

use Sirmekus\Database\Model;

$model = new Model();

By default you should create an env.php file that contains your database configurations. It is from this file that database configuration connection will be called and initialised. You should copy and place this in your root folder (with the exact keys):

define( 'DB_HOST', 'localhost' );
define( 'DB_NAME', 'your_database_name' );
define( 'DB_USER', 'your_username' );
define( 'DB_PASS', 'your_database_password' );

We encourage you to store your core database connection config in a file as suggested above. However, you can choose to pass these configurations as aruments to the class like so:

require_once 'path_to_vendor/autoload.php';

use Sirmekus\Database\Model;

$model = new Model($DB_HOST, $DB_USER, $DB_NAME, $DB_PASS);

Passing Name of Table

When using any of the CRUD methods in this class the last argument is always the name of the database table (as you'll soon see below). This is one way to always pass the name of the table at runtime.

Another way is by setting the public table property to the name of the table. Example:

require_once 'path_to_vendor/autoload.php';

use Sirmekus\Database\Model;

$model = new Model();
$model->table = "table";

And finally, you can specify the table to use by extending the Model class and giving the class the same name as the table. By default, if none of the above is specified this is used. The lower-case, snake-case name of the class will become the name of the database table to use. Example:

//Fictious Class => TestTable.php
require_once 'path_to_vendor/autoload.php';

namespace YourName\Folder;

use Sirmekus\Database\Model;

class TestTable extends Model
{
    //The table name is 'test_table'. You don't need to add any property or method (unless you wish to add)
}
//Now you can use it like you would with the "Model" class => test.php
require_once 'path_to_vendor/autoload.php';

use YourName\Folder\TestTable;

$model = new TestTable();
//name of the table will be **"test_table"** and you can use every method defined in the parent class.

You can also, if you don't like our naming convention, set the public table property to the name of the table or pass it as argument to any of the CRUD methods. To get the name of your table simply call the getTable() method on an instance of Model or any class derived from it.

Please note that you should always set the table by passing the name as argument to any of the CRUD method when called or by setting it via the table property on an instantiated class if you use the Model class directly.


Examples

  • Inserting

You can insert into table by using the insert() method of the class. This method expects a multi-dimensional array of data (specifying the table column(s) and appropriate value(s)) as first argument and an optional "table" parameter as second argument. Example:

require_once 'path_to_vendor/autoload.php';

use Sirmekus\Database\Model;

$model = new Model();
$model->insert([
    'name'=>"Sir Mekus", 
    "country"=>"Nigeria"
    ]);

//or, with name of table
$model->insert([
    'name'=>"Sir Mekus", 
    "country"=>"Nigeria"
    ], 
    $table='developers'
    );

You can also insert into the table by specifying dynamic properties at runtime, and then call the save() method. Example:

require_once 'path_to_vendor/autoload.php';

use Sirmekus\Database\Model;

$model = new Model();
$model->name = "Sir Mekus";
$model->country = "Nigeria";
$model->save();

They all do the same work effectively. You may choose to, instead of always specifying your table, extend the Model class with the name of your table as actual class name and then run your operation using this extended class.


  • Updating

To update the table call the update() method, passing it an array of data (specifying the table column(s) and appropriate value(s)) as first argument, an optional array of "where" clause(s) as second argument and an optional "table name" as the final argument. Example:

require_once 'path_to_vendor/autoload.php';

use Sirmekus\Database\Model;

$model = new Model();
$model->update([
    'name'=>"Sir Mekus", 
    "country"=>"Nigeria"
    ]);

//or
$model->update([
    'name'=>"Sir Mekus", 
    "country"=>"Nigeria"
    ], 
    [
        'email'=>'[email protected]'
    ]
    );

//or
$model->update([
    'name'=>"Sir Mekus", 
    "country"=>"Nigeria"
    ], 
    [
        'email'=>'[email protected]'
    ],
     $table='developers');

  • Updating or Creating Record

Sometimes we will like to update a record if the record/row exists in the database else insert it. You can do this by calling the updateOrCreate() method, passing a key-value array specifying the where clause - the column(s) and key(s) that makes each record unique - as the first argument; key-value array containing the table's column-value as second argument (what should be inserted or created); and then an optional table as final argument. Example:

require_once 'path_to_vendor/autoload.php';

use Sirmekus\Database\Model;

$model = new Model();

$model->updateOrCreate(
    [
        'email'=>$email
    ], 
    [
        'name' => $name, 
        'email' => $email, 
        'phone' => $tel
    ]
);

or 

$model->updateOrCreate(
    [
        'email'=>$email
    ], 
    [
        'name' => $name, 
        'email' => $email, 
        'phone' => $tel
    ], 
    $table='developers');

It searches the database for matching record using the first argument. If a record is found the table will be updated with the data provided in the second argument else a new record will be created by merging the first and second arguments.


  • Selecting Record

To select from table simply call the select() method. This method accepts an optional array as first argument, like in the above cases, but with specific keys that have special meanings; an optional array of key-value (table "column=>value") "where" clause(s) as second argument; and an optional table as final argument.

Example:

require_once 'path_to_vendor/autoload.php';

use Sirmekus\Database\Model;

$model = new Model();

$model->select(
    [
        'limit'=>20,
        'offset'=>3,
        'orderBy'=>'email',
        'groupBy'=>'location'
    ],
);

or

require_once 'path_to_vendor/autoload.php';

use Sirmekus\Database\Model;

$model = new Model();

$model->select(
    [
        'column'=>'name, email, phone_number, location',
        'limit'=>20,
        'offset'=>3,
        'orderBy'=>'email',
        'groupBy'=>'location',
        'debug'=>true,
    ],
    [
        'location' => 'Nigeria'
    ]
);

The expected array keys in the first argument are optional and they are:

  • column: A string containing columns to select (multiple columns should be coma-delimited). If not specified all the columns will be selected.

  • limit: An integer containing the limit clause

  • offset: An integer containing the offset clause

  • orderBy: A string containing the name of a particular column the result should be ordered by using the orderBy clause

  • groupBy: A string containing the name of a particular column the result should be ordered by using the groupBy clause

  • debug: If set or present the crafted query will be echoed in the script for inspection.


  • Counting Record

To count record simply use the count() method. The count() method works like the select() method above but the only possible array key is just the: column. Example:

require_once 'path_to_vendor/autoload.php';

use Sirmekus\Database\Model;

$model = new Model();

$model->count(
    [
        'column'=>'name, email, phone_number, location'
    ]
);

In the above example, the 'column' is specified. This is useful since you can pass the same config array used in a select() method call to the count() method so that only the column key can be picked (as it is all it needs).

You can also specify a where clause via the second argument of the method. Example:

require_once 'path_to_vendor/autoload.php';

use Sirmekus\Database\Model;

$model = new Model();

$model->count(
    [
        'column'=>'name, email, phone_number, location'
    ],
    [
        'email'=>'[email protected]'
    ]
);

Or, if without the first argument then it will be:

require_once 'path_to_vendor/autoload.php';

use Sirmekus\Database\Model;

$model = new Model();

$model->count(
    null,
    [
        'language' => 'php'
    ]
);

Remember that the last argument is the name of the table to query, and it's an optional argument.


  • Deleting Record

To delete a record simply call the delete() method and pass it an optional key-value array, like in other cases, as first argument and an optional table parameter value as final argument. Example:

require_once 'path_to_vendor/autoload.php';

use Sirmekus\Database\Model;

$model = new Model();

$model->delete([
        'email'=>'[email protected]'
    ]);

Note that if you've already set dynamic properties and then call this method without passing the first argument the dynamic property, or properties, added will be considered and used as the "where" clause. Example:

require_once 'path_to_vendor/autoload.php';

use Sirmekus\Database\Model;

$model = new Model();

$model->email = '[email protected]';

$model->delete();

Meanwhile

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

Patronise us on Webloit.

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.

You might also like...
⚡️ Web3 PHP is a supercharged PHP API client that allows you to interact with a generic Ethereum RPC.
⚡️ Web3 PHP is a supercharged PHP API client that allows you to interact with a generic Ethereum RPC.

Web3 PHP is a supercharged PHP API client that allows you to interact with a generic Ethereum RPC. This project is a work-in-progress. Code and docume

A simple PHP GitHub API client, Object Oriented, tested and documented.

PHP GitHub API A simple Object Oriented wrapper for GitHub API, written with PHP. Uses GitHub API v3 & supports GitHub API v4. The object API (v3) is

A simple Object Oriented PHP Client for Termii SMS API

Termii Client A simple Object Oriented PHP Client for Termii SMS API. Uses Termii API. Requirements PHP = 7.2 Guzzlehttp ~6|~7 Installation Via Compo

Xendit REST API Client for PHP - Card, Virtual Account, Invoice, Disbursement, Recurring Payments, Payout, EWallet, Balance, Retail Outlets Services

Xendit REST API Client for PHP - Card, Virtual Account, Invoice, Disbursement, Recurring Payments, Payout, EWallet, Balance, Retail Outlets Services

PHP client for Kafka

A library to allow people to communicate to Kafka using plain PHP, compatible with Kafka v0.11+ (due to the way the protocol works).

php 8 client for the lemon.markets api

lemon.markets php client This repository contains a php 8+ compatible client for the https://lemon.markets API. The documentation of the API can be fo

PHP client for Microsoft Azure Face API.

Microsoft Azure Face API PHP client A PHP library that utilizes Azure Face REST API. Requirements PHP = 7.4 Installation composer require darmen/php-

Google PHP API Client Services

Google PHP API Client Services

AltiriaSmsPhpClient, the official PHP client of Altiria
AltiriaSmsPhpClient, the official PHP client of Altiria

Altiria, cliente SMS PHP Altiria SMS PHP es un cliente que simplifica al máximo la integración de nuestro API para PHP. Por el momento, esta librería

Owner
SirMekus
I'm an experienced full-stack developer proficient in both Javascript (Vuejs, Jquery) and PHP (Laravel) respectively, and a Statistician.
SirMekus
HTTP Requestor: Package for a client request that supports you to make an external service request easily and with fast usage.

HttpRequestor from Patienceman HTTP Requestor: Package for a client request that supports you to make an external service request easily and with fast

Manirabona Patience 2 Aug 26, 2022
Adds a specific header to every response to disable Google's usage of your site in it's FLoC tracking method.

Go Unfloc Yourself Description A bundle for Symfony 5 that adds a Permissions-Policy header in all the responses to prevent the use of new Google's "F

(infinite) loophp 3 Feb 25, 2022
A tool for sending fast and managed messages to Telegram bot users

???? HiToAll A tool for sending fast and managed messages to Telegram bot users About In some telegram bots programmed with php language, if there are

Matin Najafi 3 Oct 3, 2021
A Discord bot that rewards players in Minecraft for sending messages on Discord

MCRewards-Discord-Bot A Discord bot that rewards players in Minecraft for sending messages on Discord How to setup: Download this git as a .zip, or cl

null 3 Dec 26, 2021
Google-api-php-client - A PHP client library for accessing Google APIs

Google APIs Client Library for PHP Reference Docs https://googleapis.github.io/google-api-php-client/main/ License Apache 2.0 The Google API Client Li

Google APIs 8.4k Dec 30, 2022
PHP JSON-RPC 2.0 Server/Client Implementation with Automatic Client Class Generation via SMD

PHP JSON-RPC 2.0 Server/Client Implementation with Automatic Client Class Generation via SMD

Sergey Bykov 63 Feb 14, 2022
OpenAI API Client is a component-oriented, extensible client library for the OpenAI API. It's designed to be faster and more memory efficient than traditional PHP libraries.

OpenAI API Client in PHP (community-maintained) This library is a component-oriented, extensible client library for the OpenAI API. It's designed to b

Mounir R'Quiba 6 Jun 14, 2023
Laravel ClickHouse adds CH client integration, generation & execution of ClickHouse database migrations to the Laravel application.

Laravel ClickHouse Introduction Laravel ClickHouse database integration. This package includes generation and execution of the ClickHouse database mig

cybercog 11 Dec 20, 2022
🤖 Telegram Bot API PHP SDK. Lets you build Telegram Bots easily! Supports Laravel out of the box.

Telegram Bot API - PHP SDK Telegram Bot PHP SDK lets you develop Telegram Bots in PHP easily! Supports Laravel out of the box. Telegram Bot API is an

Irfaq Syed 2.5k Jan 6, 2023
This library allows you to quickly and easily use the Twilio SendGrid Web API v3 via PHP

This library allows you to quickly and easily use the Twilio SendGrid Web API v3 via PHP

Twilio SendGrid 1.4k Dec 27, 2022