PHPackages                             laswitchtech/php-api - 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. [API Development](/categories/api)
4. /
5. laswitchtech/php-api

ActiveLibrary[API Development](/categories/api)

laswitchtech/php-api
====================

REST API for php applications

v2.0.17(1y ago)0921GPL-3.0-onlyPHP

Since Oct 31Pushed 1y ago1 watchersCompare

[ Source](https://github.com/LouisOuellet/php-api)[ Packagist](https://packagist.org/packages/laswitchtech/php-api)[ RSS](/packages/laswitchtech-php-api/feed)WikiDiscussions stable Synced 1mo ago

READMEChangelog (10)Dependencies (2)Versions (86)Used By (0)

[![GitHub repo logo](/dist/img/logo.png)](/dist/img/logo.png)

phpAPI - \[DEPRECATED\] - Use [coreAPI](https://github.com/LaswitchTech/coreAPI) instead
========================================================================================

[](#phpapi---deprecated---use-coreapi-instead)

[![License](https://camo.githubusercontent.com/9ef2982e4ae56c5e7f011ce6dc9396a52c139cd1cf2bbfd4f9b34a7c15ca1d1c/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f4c6f7569734f75656c6c65742f7068702d6170693f7374796c653d666f722d7468652d6261646765)](https://camo.githubusercontent.com/9ef2982e4ae56c5e7f011ce6dc9396a52c139cd1cf2bbfd4f9b34a7c15ca1d1c/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f4c6f7569734f75656c6c65742f7068702d6170693f7374796c653d666f722d7468652d6261646765)[![GitHub repo size](https://camo.githubusercontent.com/f1350a6f7b79bb801833d8b28eb7b714888d50333991289dd53adbb5126e5692/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f7265706f2d73697a652f4c6f7569734f75656c6c65742f7068702d6170693f7374796c653d666f722d7468652d6261646765266c6f676f3d676974687562)](https://camo.githubusercontent.com/f1350a6f7b79bb801833d8b28eb7b714888d50333991289dd53adbb5126e5692/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f7265706f2d73697a652f4c6f7569734f75656c6c65742f7068702d6170693f7374796c653d666f722d7468652d6261646765266c6f676f3d676974687562)[![GitHub top language](https://camo.githubusercontent.com/b1aa4926f982fff0a49a018862979362e1eaf7cc9fa3517ecc4b7c89be975cfe/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c616e6775616765732f746f702f4c6f7569734f75656c6c65742f7068702d6170693f7374796c653d666f722d7468652d6261646765)](https://camo.githubusercontent.com/b1aa4926f982fff0a49a018862979362e1eaf7cc9fa3517ecc4b7c89be975cfe/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c616e6775616765732f746f702f4c6f7569734f75656c6c65742f7068702d6170693f7374796c653d666f722d7468652d6261646765)[![Version](https://camo.githubusercontent.com/0791e6f9d2438e4b7822f5bf616cabc45e8bc54fdbbf2fe0fd2808e6ee351bfa/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f762f72656c656173652f4c6f7569734f75656c6c65742f7068702d6170693f6c6162656c3d56657273696f6e267374796c653d666f722d7468652d6261646765)](https://camo.githubusercontent.com/0791e6f9d2438e4b7822f5bf616cabc45e8bc54fdbbf2fe0fd2808e6ee351bfa/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f762f72656c656173652f4c6f7569734f75656c6c65742f7068702d6170693f6c6162656c3d56657273696f6e267374796c653d666f722d7468652d6261646765)

Features
--------

[](#features)

- REST API

Why you might need it
---------------------

[](#why-you-might-need-it)

If you are looking for an easy start for your PHP REST API. Then this PHP Class is for you.

Can I use this?
---------------

[](#can-i-use-this)

Sure!

License
-------

[](#license)

This software is distributed under the [GNU General Public License v3.0](https://www.gnu.org/licenses/gpl-3.0.en.html) license. Please read [LICENSE](LICENSE) for information on the software availability and distribution.

Requirements
------------

[](#requirements)

- PHP &gt;= 8.0
- MySQL or MariaDB

Security
--------

[](#security)

Please disclose any vulnerabilities found responsibly – report security issues to the maintainers privately.

Installation
------------

[](#installation)

Using Composer:

```
composer require laswitchtech/php-api
```

How do I use it?
----------------

[](#how-do-i-use-it)

In this documentations, we will use a table called users for our examples.

### Skeleton

[](#skeleton)

Let's start with the skeleton of your API project directory.

```
├── api.php
├── config
│   └── api.cfg
├── Controller
│   └── UserController.php
└── Model
    └── UserModel.php
```

- api.php: The api file is the entry-point of our application. It will initiate the controller being called in our application.
- config/api.cfg: The config file holds the configuration information of our API. Mainly, it will hold the database credentials. But you could use it to store other configurations.
- Controller/: This directory will contain all of your controllers.
- Controller/UserController.php: the User controller file which holds the necessary application code to entertain REST API calls. Mainly the methods that can be called.
- Model/: This directory will contain all of your models.
- Model/UserModel.php: the User model file which implements the necessary methods to interact with the users table in the MySQL database.

### Models

[](#models)

Model files implements the necessary methods to interact with a table in the MySQL database. These model files needs to extend the Database class in order to access the database.

#### Naming convention

[](#naming-convention)

The name of your model file should start with a capital character and be followed by `Model.php`. If not, the bootstrap will not load it. The class name in your Model files should match the name of the model file.

#### Example

[](#example)

```
//Import BaseModel class into the global namespace
use LaswitchTech\phpAPI\BaseModel;

class UserModel extends BaseModel {
  public function getUsers($limit) {
    return $this->select("SELECT * FROM users ORDER BY id ASC LIMIT ?", ["i", $limit]);
  }
}
```

### Controllers

[](#controllers)

Controller files holds the necessary application code to entertain REST API calls. Mainly the methods that can be called. These controller files needs to extend the BaseController class in order to access the basic methods.

#### Naming convention

[](#naming-convention-1)

The name of your controller file should start with a capital character and be followed by `Controller.php`. If not, the bootstrap will not load it. The class name in your Controller files should match the name of the controller file.

Finally, callable methods need to end with `Action`.

#### Example

[](#example-1)

```
//Import BaseController class into the global namespace
use LaswitchTech\phpAPI\BaseController;

class UserController extends BaseController {

  public function __construct($Auth){

    // Set the controller Authentication Policy
    $this->Public = true; // Set to false to require authentication

    // Set the controller Authorization Policy
    $this->Permission = false; // Set to true to require a permission for the namespace used. Ex: namespace>/user/list
    $this->Level = 1; // Set the permission level required

    // Call the parent constructor
    parent::__construct($Auth);
  }

  public function listAction() {
    try {

      // Namespace: /user/list

      // Check the request method
      if($this->Method !== 'GET'){
        throw new Error('Invalid request method.');
      }

      // Initialize the user model
      $UserModel = new UserModel();

      // Configure default limit
      $Limit = 25;

      // Check if the limit is set
      if($this->getQueryStringParams('limit')){
        $Limit = intval($this->getQueryStringParams('limit'));
      }

      // Get the users
      $Users = $UserModel->getUsers($Limit);

      // Check if the users were found
      if(count($Users) output(
        $Users,
        array('Content-Type: application/json', 'HTTP/1.1 200 OK')
      );
    } catch (Error $e) {

      // Set the error
      $this->Error = $e->getMessage();

      // Log the error
      $this->Logger->error($e->getMessage());

      // Send the output
      $this->output(
        array('error' => $this->Error . ' - Something went wrong! Please contact support.'),
        array('Content-Type: application/json', 'HTTP/1.1 500 Internal Server Error'),
      );
    }
  }
}
```

### Configurations

[](#configurations)

The config file holds the configuration information of our API. Mainly, it will hold the database credentials. But you could use it to store other configurations. The configuration file must be stored in config/config.php. As this file is already being loaded in the bootstrap.

#### Example

[](#example-2)

```
{
    "sql": {
        "host": "localhost",
        "database": "demo3",
        "username": "demo",
        "password": "demo"
    }
}
```

### API

[](#api)

The api file is the entry-point of our application. It will initiate the controller being called in our application. The file itself can be named any way you want. As long as you point your API calls to it. In our example we use api.php. This name is useful because it allows you to build a front-end using the index.php file and it also makes it obvious as the URL of your API.

#### Example

[](#example-3)

```
// Initiate Session
session_start();

// These must be at the top of your script, not inside a function
use LaswitchTech\phpAPI\phpAPI;

// Load Composer's autoloader
require 'vendor/autoload.php';

// Initiate phpAPI
new phpAPI();
```

### Calling the API

[](#calling-the-api)

Once you have setup your first controller and model, you can start calling your api.

### JavaScript Implementation

[](#javascript-implementation)

phpAPI comes packed with a JavaScript implementation. The class is available in /vendor/laswitchtech/php-api/dist/js/phpAPI.js.

### Examples

[](#examples)

For more example, look into the [example](example) folder.

### Installer Example

[](#installer-example)

```
// Initiate Session
session_start();

// These must be at the top of your script, not inside a function
use LaswitchTech\phpLogger\phpLogger;
use LaswitchTech\phpSMS\phpSMS;
use LaswitchTech\SMTP\phpSMTP;
use LaswitchTech\phpDB\Database;
use LaswitchTech\phpAUTH\phpAUTH;

// Load Composer's autoloader
require 'vendor/autoload.php';

// Initiate phpLogger
$phpLogger = new phpLogger();

// Configure phpLogger
$phpLogger->config("level",0); // Set Logging Level

// Initiate phpSMS
$phpSMS = new phpSMS();

// Configure phpSMS
$phpSMS->config('provider','twilio')
       ->config('sid', 'your_account_sid')
       ->config('token', 'your_auth_token')
       ->config('phone', 'your_twilio_phone_number');

// Initiate phpDB
$phpDB = new Database();

// Configure phpDB
$phpDB->config("host","localhost")
      ->config("username","demo")
      ->config("password","demo")
      ->config("database","demo3");

// Initiate phpSMTP
$phpSMTP = new phpSMTP();

// Configure phpSMTP
$phpSMTP->config("username","username@domain.com")
        ->config("password","*******************")
        ->config("host","smtp.domain.com")
        ->config("port",465)
        ->config("encryption","ssl");

// Construct Hostnames
$Hostnames = ["localhost","::1","127.0.0.1"];
if(isset($_SERVER['SERVER_NAME']) && !in_array($_SERVER['SERVER_NAME'],$Hostnames)){
  $Hostnames[] = $_SERVER['SERVER_NAME'];
}
if(isset($_SERVER['HTTP_HOST']) && !in_array($_SERVER['HTTP_HOST'],$Hostnames)){
  $Hostnames[] = $_SERVER['HTTP_HOST'];
}

// Initiate phpAUTH
$phpAUTH = new phpAUTH();

// Configure phpAUTH
$phpAUTH->config("hostnames",$Hostnames)
        ->config("basic",false) // Enable/Disable Basic Authentication
        ->config("bearer",true) // Enable/Disable Bearer Token Authentication
        ->config("request",false) // Enable/Disable Request Authentication
        ->config("cookie",false) // Enable/Disable Cookie Authentication
        ->config("session",false) // Enable/Disable Session Authentication
        ->config("2fa",false) // Enable/Disable 2-Factor Authentication
        ->config("maxAttempts",5) // Max amount of authentication attempts per windowAttempts
        ->config("maxRequests",1000) // Max amount of API request per windowRequests
        ->config("lockoutDuration",1800) // 30 mins
        ->config("windowAttempts",100) // 100 seconds
        ->config("windowRequests",60) // 60 seconds
        ->config("window2FA",60) // 60 seconds
        ->config("windowVerification",2592000) // 30 Days
        ->init();

// Install phpAUTH
$Installer = $phpAUTH->install();

// Create a User
$User = $Installer->create("api",["username" => "username@domain.com"]);

// Activate User
$User->activate();

// Verify User
$User->verify();

// Initiate phpConfigurator
$Configurator = new phpConfigurator('account');

// Save Account for Testing
$Configurator->set('account','url',"https://{$Hostname}/api.php")
             ->set('account','token',$User->get('username').":".$User->getToken());
```

###  Health Score

31

—

LowBetter than 68% of packages

Maintenance32

Infrequent updates — may be unmaintained

Popularity10

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity62

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~7 days

Recently: every ~17 days

Total

85

Last Release

706d ago

Major Versions

v1.5.14 → v2.0.12023-04-13

PHP version history (3 changes)v1.0PHP &gt;=8.0

v1.3.0PHP ^8.0

v1.5.5PHP \*

### Community

Maintainers

![](https://www.gravatar.com/avatar/1fbdd49d98caf05990260b5ece6a0e9539ca2d448b4af10ebde6ed7d6688c61d?d=identicon)[LouisOuellet](/maintainers/LouisOuellet)

---

Top Contributors

[![LouisOuellet](https://avatars.githubusercontent.com/u/25933907?v=4)](https://github.com/LouisOuellet "LouisOuellet (134 commits)")

### Embed Badge

![Health badge](/badges/laswitchtech-php-api/health.svg)

```
[![Health](https://phpackages.com/badges/laswitchtech-php-api/health.svg)](https://phpackages.com/packages/laswitchtech-php-api)
```

###  Alternatives

[twilio/sdk

A PHP wrapper for Twilio's API

1.6k92.9M272](/packages/twilio-sdk)[facebook/php-business-sdk

PHP SDK for Facebook Business

90821.9M34](/packages/facebook-php-business-sdk)[meilisearch/meilisearch-php

PHP wrapper for the Meilisearch API

74513.7M114](/packages/meilisearch-meilisearch-php)[google/common-protos

Google API Common Protos for PHP

173103.7M50](/packages/google-common-protos)[hubspot/api-client

Hubspot API client

23914.2M16](/packages/hubspot-api-client)[lstrojny/fxmlrpc

Fast and tiny XML/RPC client with bridges for various HTTP clients

1425.4M30](/packages/lstrojny-fxmlrpc)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
