How useful is the pharapi open source interface framework? The most complete back-end PHP development strategy

Posted by literom on Tue, 18 Jan 2022 14:01:12 +0100

Pharapi open source interface framework

Pronunciation: Pai framework, official website:

PhalApi is a lightweight open source interface framework for PHP, which is committed to the rapid development of interface services. It supports protocols such as HTTP/SOAP/RPC and can be used to build interfaces / micro services / RESTful interfaces / Web Services. It promises to be permanently free and can be used for commercial purposes.

It supports automatic generation of interface documents, automatic parameter verification, automatic generation of unit test code, automatic possession of CURD data interface and automatic installation program, making interface development simpler, more efficient and more professional.

Screenshot of PhalApi official website:

Online example

Using pharapi open source interface framework, you can quickly develop and write your API interface, and automatically generate online interface documents.

Online example:

  • Online interface documentation:
  • Interface details document (take the default interface as an example):
  • Default interface service:

Pharapi will automatically generate online interface documents in real time according to the parameter configuration and code comments of the interface you write. The effect of the interface list page is similar to the following:

Pharapi also supports online interface testing, requesting sample descriptions, generating offline HTML interface documents, and real-time updates. The effect of interface document details page is similar as follows:

express setup

One click installation of composer

Using the command of composer to create a project can realize one click installation.

$ composer create-project phalapi/phalapi

Warm tip: for the use of Composer, please refer to Composer Chinese website / packgist China full volume image.

Manual download and installation

Alternatively, it can be installed manually. After downloading and decompressing the Git project code, update the optional composer, that is:

$ composer update


Nginx configuration

If using Nginx, refer to the following configuration.

server {
    listen 80;
    # Set the root directory to the public directory
    root /path/to/phalapi/public;
    charset utf-8;

    location / {
        index index.php;

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        # Select the appropriate communication mode according to the current environment
        # fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

When configuring, you need to set the website root directory to the public directory. After saving the configuration, restart nginx.

Warm tip: it is recommended to point the access root path to / path/to/phalapi/public.

Database configuration

How to use MySQL database, refer to modify/ config/dbs.php database configuration.

return array(
     * DB Database server cluster
    'servers' => array(
        'db_master' => array(                       // Server tag / database identify
            'type'      => 'mysql',                 // Database type. Only mysql, sqlserver / database type are supported temporarily
            'host'      => '',             // Database domain name / database host
            'name'      => 'phalapi',               // database name
            'user'      => 'root',                  // Database user name / database user
            'password'  => '',                      // database password
            'port'      => 3306,                    // database port
            'charset'   => 'UTF8',                  // database charset
            'pdo_attr_string'   => false,           // Database query results use strings uniformly. true is yes, false is No
            'driver_options' => array(              // Connection option configuration during PDO initialization
                // For more configurations, please refer to the official documentation:

    // More code omitted

Finally, you need to add write permissions to the runtime directory.

Operation and use

Call interface

In pharapi, you can specify the interface service to be called through the service parameter (the short name is the s parameter). For example, access the default interface service.

The result output after the interface request is similar to the following:

    "ret": 200,
    "data": {
        "title": "Hello PhalApi",
        "version": "2.4.2",
        "time": 1501079142
    "msg": ""

The PHP code corresponding to the execution is in/ src/app/Api/Site.php file, the source code fragment is as follows:

namespace App\Api;
use PhalApi\Api;

 * Default interface service class
 * @author: dogstar <> 2014-10-04
class Site extends Api {
    public function getRules() {
        return array(
            'index' => array(
                'username'  => array('name' => 'username', 'default' => 'PhalApi', 'desc' => 'user name'),

     * Default interface service
     * @desc Default interface service, which is executed when no interface service is specified
     * @return string title title
     * @return string content content
     * @return string version Version, format: X.X.X
     * @return int time Current timestamp
     * @exception 400 Illegal request, parameter passing error
    public function index() {
        return array(
            'title' => 'Hello ' . $this->username,
            'version' => PHALAPI_VERSION,
            'time' => $_SERVER['REQUEST_TIME'],

The screenshot of the operation effect is as follows:

The partial screenshot of the automatically generated interface document details page is as follows:


Modification/ public/init.php file to set the current language.

// Translation language pack settings - Simplified Chinese

// Setting language to English

A picture shows you how to use phalapi 2 x

PhalApi development documentation

The elegant and detailed development documents specially prepared for PHPer can basically find the answers you want in the documents. Please see: phalapi 2 X development documentation, .

The document is written in markdown, and the screenshot of the document page is as follows:

Development document outline:

PhalApi 2.x Development documentation

        How to upgrade PhalApi?

    1, Rapid development
        1.1 Download and install
        1.2 function Hello World
        1.3 How to request interface services
        1.4 Interface response and online debugging
        1.5 Api Interface layer
        1.6 DataApi Universal data interface
        1.7 Domain Domain layer and ADM pattern
        1.8 Model Data layer and database operation
        1.9 DataModel Data base class
        1.10 unit testing 
        1.11 Automatic loading and PSR-4
        1.12 Interface documentation
        1.13 initialization

    2, Database
        2.1 Database connection
        2.2 Database and NotORM
        2.3 Database usage and query
        2.4 Database database and table division strategy
        2.5 Connect to multiple databases
        2.6 Print and save SQL sentence
        2.7 Customize your Model Base class

    3, Advanced topics
        3.1 Interface parameters
        3.2 to configure
        3.3 journal
        3.4 cache
        3.5 filter(Interface signature)
        3.6 COOKIE
        3.7 encryption
        3.8 internationalization
        3.9 CURL request
        3.10 Tools and miscellaneous
        3.11 DI Service summary
        3.12 Extended class library
        3.13 SDK Use of packages
        3.14 Script command
        3.15 MQ queue
        3.16 error handling

2020 video tutorial

Starting from station B, lesson 1 ~ Lesson 11 are equipped with the outline of video knowledge points of each lesson.

Video course:

    PhalApi 2020 Video tutorial
        Lesson one B The station starts, and the 2020 video tutorial begins!
        Lesson 2 video tutorial - Download and install
        Lesson 3 video tutorial - Hello World
        Lesson 4 video tutorial - How to request interface services
        Lesson 5 video tutorial - Interface response and online debugging
        Lesson 6 video tutorial - Api Interface layer
        Lesson 7 video tutorial - Domain Domain business layer and ADM Mode interpretation
        Lesson 8 video tutorial - Model Data layer and database connection
        Lesson 9 video tutorial - Test driven development and PHPUnit
        Lesson 10 video tutorial - Automatic loading and PSR-4
        Lesson 11 video tutorial - Interface documentation
        Video tutorial - Ten minute experience PhalApi Pro,Give Way PHP Interface development is more interesting!
        Video tutorial - Tea shop application practice

Related books

E-book: getting to know PhalApi: exploring the skills of interface service development

Author: Huang Zen

Turing community link:


 Part I exploration
 Chapter 1 meeting PhalApi
 Chapter 2 basic introduction
 Chapter 3 Advanced Topics
 Chapter 4 is not just coding
 Part II project cases
 Chapter 5 new entrepreneurial projects
 Chapter 6 rewriting historical legacy projects
 Chapter 7 an extreme project
 The third part goes further
 Chapter 8 PhalApi Perfect interpretation
 Chapter 9 how to effectively design the interface framework
 Chapter 10 open source
 appendix A Interface service document template

E-book: good quality! PHP enterprise system development

Author: Huang Zen

Turing community link:


Recommendation order
 Part I redefining project development
 Chapter 1 essence of software development
 Chapter 2 selecting a high starting point
 Chapter 3 release your code to the world
 Part II PHP Advanced programming
 Chapter 4 return to the original ecology
 Chapter 5 PHPUnit New solution of unit test
 Part III PHP Enterprise system development
 Chapter 6 core basic module design
 Chapter 7 large website development paradigm
 Chapter 8 high availability interface service system
 Chapter 9 ultimate management background system
 Chapter 10 hidden mission planning system
 The fourth part is the pursuit of excellence
 Chapter 11 how to become a star employee
 Chapter 12 winning in role transformation


If you are interested in the internal implementation, source code and technical architecture of the PhalApi open source framework, you can view the following subprojects.

  • Phalapi / kernel framework kernel
  • phalapi/notorm database package

PhalApi 2. The system architecture of version x is as follows:

PhalApi composer extension

  • Phalapi / authauthauth permission extension
  • Phalapi / cli extended class library
  • Phalapi / fast routefastroute fast route
  • Phalapi aliyun ossphalapi OSS alicloud OSS package
  • phalapi/PHPMailer email sending based on PHPMailer
  • phalapi/qiniu qiniu cloud storage interface call
  • phalapi/qrcodePhalApi QR code extension
  • phalapi/pinyinPhalApi 2.x Pinyin extension
  • phalapi/jwt JWT extension based on PhalApi2
  • Chenall / phalapi Weixin wechat extension
  • phalapi/wechatmini wechat applet extension
  • Phalapi / Ding com BOT nail enterprise internal webhook robot extension
  • phalapi-pay supports WeChat payment and Alipay payment

Warm tip: the above extensions need to be installed through composer before use. For more use and development of extended class libraries, please refer to the document: PhalApi framework extended class library: .

PhalApi application plug-in

  • Operation platform plug-in
  • Application user plug-in
  • Encryption and decryption plug-in
  • Alipay plug in
  • Tea shop wechat applet application
  • Online interface document theme pack

Warm tip: the difference between application plug-ins and composer extensions is that application plug-ins are more granular and have more specific functions. They may not only have databases, interfaces, interfaces, but also cooperate with other terminals, and are not subject to the specification constraints of composer. They are the development method independently invented and designed by PhalApi. For more information, please refer to the third party application plug-in development tutorial:

Technical products developed based on PhalApi

The following products use the PhalApi open source framework and are officially self-developed products. Individuals / teams / enterprises are welcome to use them.

Interface master - build your interface open platform immediately (original name: PhalApi Professional Edition)

Product official website:

Guochuang cloud - backend low code development platform

Product official website:

YesDev collaboration cloud - collaborate all your projects online

Product official website:

PhalApi 2. Perfect interpretation of X version

2.x system architecture

It is mainly divided into three layers:

  • In the application layer of phalapi/phalapi project, you can use phalapi/phalapi to build microservices, interface systems, RESTful, web services, etc.
  • Extended class library extended class library refers to optional and reusable components or class libraries, which can be directly integrated and used, maintained and shared by developers, corresponding to the original phalapi library project.
  • The core framework is divided into two parts: the core part of PhalApi, kernal, and the optimized notorm.

Among them, the respective composer and github projects are:







Extended class library

It is maintained and shared by the majority of developers. composer recommends that you register uniformly with phalapi.

It is maintained and shared by developers, and the source code can be maintained in their Github warehouse.

Core framework



UML static structure diagram of the core part of the framework

PhalApi 2. The UML static structure diagram of the core framework of version x, HD version is as follows:

Firstly, the PhalApi\PhalApi class in the green part is the access portal of the whole interface system, which is the key to the use of the project application system and client. For the relevant calling code, you can refer to the implementation code fragment of the unified entry file.

$pai = new \PhalApi\PhalApi();

Only two lines of code are needed to complete the request response to the interface service.

Secondly, there are three layers of Api, Domain and Model in the yellow part, that is, the ADM layered architecture. This part needs developers' attention, because it is also the part that specific project development needs to be implemented by itself.

Finally, the red part of DI dependency injection is the core of the whole framework. Not only in the core framework, but also in the project application.

Core execution flow sequence diagram

PhalApi 2.x version of the core execution flow sequence diagram, and 1 The X version is basically the same. It can be seen that no matter how the technology is upgraded, the initial core timing process of phalapi remains dynamic. The only change is the name of each class.

Community communication

Send framework pharapi communication group QQ group (1165 people)

Group No.: 421032344

Send framework - PhalApi communication group 2-QQ group (830 people)

Group No.: 459352221

PhalApi technology exchange group - wechat group (83 people)

Add micro signal: dogstarhuang into the group

Code warehouse

Gitee code cloud - pharapi open source interface framework (1.7k Star)

Code link:

GitHub - pharapi open source interface framework (1.4 Star)

Code link:

Version update

Phalapi version 2.17.2 was released and updated in July 2021.

Phalapi version 2.17.2

[BUG fix]

1. For SQL records, only part of the necessary parameters are extracted to avoid all records, and to avoid recording sensitive information such as passwords to log files

2. Translation and DataApi parameter description supplement

3. The exception prompt information when the DataModel calls a method that does not exist, and remove the redundant dollar symbol

4. Judge and adjust the online interface document template to avoid warning

For more historical version update journals, please see:

Topics: PHP interface Agile development