
A Faster Lightweight Full-Stack PHP Framework
中文版
Docker env
Just one command to build all env for the easy-php

How to build a PHP framework by ourself ?
Why do we need to build a PHP framework by ourself? Maybe the most of people will say "There have so many PHP frameworks be provided, but we still made a wheel?". My point is "Made a wheel is not our purpose, we will get a few of knowledge when making a wheel which is our really purpose".
Then, how to build a PHP framework by ourself? General process as follows:
Entry file ----> Register autoload function
----> Register error(and exception) function
----> Load config file
----> Request
----> Router
----> (Controller <----> Model)
----> Response
----> Json
----> View
In addition, unit test, nosql support, api documents and some auxiliary scripts, e.g. Finnally, My framework directory as follows:
Project Directory Structure
app [application backend directory]
├── demo [module directory]
│ ├── controllers [controller directory]
│ │ └── Index.php [default controller class file]
│ ├── logics [logic directory]
│ │ ├── exceptions [exception directory]
│ │ ├── gateway [a gateway example]
│ │ ├── tools [tool class directory]
│ │ └── UserDefinedCase.php [register user defined handle before framework loading router]
│ └── models [model directory]
│ └── TestTable.php [model class file]
├── config [config folder]
│ ├── demo [module config folder]
│ │ ├── config.php [module-defined config]
│ │ └── route.php [module-defined router]
│ ├── common.php [common config]
│ ├── database.php [database config]
│ ├── swoole.php [swoole config]
│ └── nosql.php [nosql config]
docs [api document directory]
├── apib [Api Blueprint]
│ └── demo.apib [api doc example file]
├── swagger [swagger]
framework [easy-php framework directory]
├── exceptions [core exception class]
│ ├── CoreHttpException.php[http exception]
├── handles [handle class file be used by app run]
│ ├── Handle.php [handle interface]
│ ├── ErrorHandle.php [error handle class]
│ ├── ExceptionHandle.php [exception handle class]
│ ├── ConfigHandle.php [config handle class]
│ ├── NosqlHandle.php [nosql handle class]
│ ├── LogHandle.php [log handle class]
│ ├── UserDefinedHandle.php[user defined handle class]
│ ├── RouterSwooleHan... [router handle class for swoole mode]
│ └── RouterHandle.php [router handle class]
├── orm [datebase object relation map class directory]
│ ├── Interpreter.php [sql Interpreter class]
│ ├── DB.php [database operation class]
│ ├── Model.php [data model]
│ └── db [db type directory]
│ └── Mysql.php [mysql class file]
├── router [router strategy]
│ ├── RouterInterface.php [router strategy interface]
│ ├── General.php [general strategy class]
│ ├── Pathinfo.php [pathinfo strategy class]
│ ├── Userdefined.php [userdefined strategy class]
│ ├── Micromonomer.php [micromonomer strategy class]
│ ├── Job.php [job strategy class]
│ ├── EasySwooleRouter.php [router strategy entrance class for swoole mode]
│ └── EasyRouter.php [router strategy entrance class]
├── nosql [nosql directory]
│ ├── Memcahed.php [memcahed class file]
│ ├── MongoDB.php [mongoDB class file]
│ └── Redis.php [redis class file]
├── App.php [this application class file]
├── Container.php [container class file]
├── Helper.php [helper class file]
├── Load.php [autoload class file]
├── Request.php [request object class file]
├── Response.php [response object class file]
├── run.php [run this application script file]
├── swoole.php [init the framework && swoole server]
frontend [application frontend source code directory]
├── src [source folder]
│ ├── components [vue components]
│ ├── views [vue views]
│ ├── images [images folder]
│ ├── ...
├── app.js [vue root js]
├── app.vue [vue root component]
├── index.template.html [frontend entrance template file]
├── store.js [vuex store file]
├── .babelrc [babel config file]
├── webpack.config.js [webpack config file]
├── yarn.lock [yarn lock file]
jobs [Jobs folder, where write you business script]
├── demo [Module folder]
│ ├── Demo.php [Job script example file]
│ ├── ...
public [this is a resource directory to expose service resource]
├── dist [frontend source file after build]
│ └── ...
├── index.html [entrance html file]
├── index.php [entrance php script file]
├── server.php [init the server with swoole]
runtime [temporary file such as log]
├── logs [log directory]
├── build [phar directory build by build script]
tests [unit test directory]
├── demo [module name]
│ └── DemoTest.php [test class file]
├── TestCase.php [phpunit test case class file]
vendor [composer vendor directory]
.git-hooks [git hooks directory]
├── pre-commit [git pre-commit example file]
├── commit-msg [git commit-msg example file]
bin [the auto script folder]
├── build [build php code to phar file script]
├── cli [run this framework with the php cli mode]
├── run [quick start script]
.env.example [the environment variables example file]
.gitignore [git ignore config file]
.travis.yml [travis-ci config file]
LICENSE [lincese file]
logo.png [logo picture]
composer.json [composer file]
composer.lock [composer lock file]
package.json [dependence file for frontend]
phpunit.xml [phpunit config file]
README-CN.md [readme file chinese]
README.md [readme file]
Life Cycle

Framework Module Description:
Entrance file
Defined a entrance file that provide a uniform file for user visit, which hide the complex logic like the enterprise service bus.
// require the application run file
require('../framework/run.php');
[file: public/index.php]
Autoload Module
Register a autoload function in the __autoload queue by used spl_autoload_register, after that, we can use a class by namespace and keyword 'use'.
[file: framework/Load.php]
Error&Exception Handle Module
Register a function by used set_error_handler to handle error, but it can't handle the following error, E_ERROR, E_PARSE, E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_ERROR, E_COMPILE_WARNING and the E_STRICT produced by the file which called set_error_handler function. So, we need use register_shutdown_function and error_get_last to handle this finally error which set_error_handler can't handle. When the framework running, we can handle the error by ourself, such as, give a friendly error messge for client.
[file: framework/hanles/ErrorHandle.php]
Register a function by used set_exception_handler to handle the exception which is not be catched, which can give a friendly error messge for client.
[file: framework/hanles/ExceptionHandle.php]
Config Handle Module
Loading framework-defined and user-defined config files.
For example,the master-salve database config:
[database]
dbtype = mysqldb
dbprefix = easy
dbname = easyphp
dbhost = localhost
username = easyphp
password = easyphp
slave = 0,1
[database-slave-0]
dbname = easyphp
dbhost = localhost
username = easyphp
password = easyphp
[database-slave-1]
dbname = easyphp
dbhost = localhost
username = easyphp
password = easyphp
[file: framework/hanles/ConfigHandle.php]
Request&Response Module
- Request Object: contains all the requested information.
- Response Object: contains all the response information.
All output is json in the framework, neithor framework's core error or business logic's output, beacuse I think is friendly.
Request param check, Support require/length/number check at present. Use as follows:
$request = App::$container->get('request');
$request->check('username', 'require');
$request->check('password', 'length', 12);
$request->check('code', 'number');
[file: framework/Request.php]
[file: framework/Response.php]
Route Handle Module
├── router [datebase object relation map class directory]
├── RouterInterface.php [router strategy interface]
├── General.php [general strategy class]
├── Pathinfo.php [pathinfo strategy class]
├── Userdefined.php [userdefined strategy class]
├── Micromonomer.php [micromonomer strategy class]
├── Job.php [job strategy class]
└── EasyRouter.php [router strategy entrance class]
Execute the target controller's function by the router parse the url information.Is composed of four types of:
tradition router
domain/index.php?module=Demo&contoller=Index&action=test&username=test
pathinfo router
domain/demo/index/modelExample
user-defined router
// config/moduleName/route.php, this 'this' point to RouterHandle instance
$this->get('v1/user/info', function (Framework\App $app) {
return 'Hello Get Router';
});
micro monolith router
What's the micro monolith router? There are a lot of teams are moving in the SOA service structure or micro service structure, I think it is difficult for a small team. So the micro monolith was born, what's this? In my opinion, this is a SOA process for a monolith application.For example:
app
├── UserService [user service module]
├── ContentService [content service module]
├── OrderService [order service module]
├── CartService [cart service module]
├── PayService [pay service module]
├── GoodsService [goods service module]
└── CustomService [custom service module]
As above, we implemented a easy micro monolith structure.But how these module to communicate with each other? As follows:
App::$app->get('demo/index/hello', [
'user' => 'TIGERB'
]);
So we can resolve this problem loose coupling. In the meantime, we can exchange our application to the SOA structure easily, beacuse we only need to change the method get implementing way in the App class, the way contain RPC, REST. etc.
[file: framework/hanles/RouterHandle.php]
MVC To MCL
The tradition MVC pattern includes the model,view,controller layer. In general, you always write the business logic in the controller or model layer. But you will feel the code is difficult to read, maintain, expand after a long time. So I add a logic layer in the framework forcefully where you can implement the business logic by yourself. You can not only implement a tool class but also implement your business logic in a new subfolder, what's more, you can implement a gateway based on the pattern of responsibility (I provided a example).
In the end, the structure as follows:
- M: models, the map of database's table where define the curd operation.
- C: controllers, where expose the business resourse
- L: logics, where implement the business logic flexiblly
Logics layer
A gateway example:
I built a gateway in the logics folder, structure as follows: