dueto dos sonhos

Procuramos formas majestosas, cores bucólicas, uma melodia harmoniosa e a nossa melhor performance para o ato. Elevamos a solidão ao seu máximo, com o estigma da martirização apoiado nos nossos…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




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:

In addition, unit test, nosql support, api documents and some auxiliary scripts, e.g. Finnally, My framework directory as follows:

Defined a entrance file that provide a uniform file for user visit, which hide the complex logic like the enterprise service bus.

Loading framework-defined and user-defined config files.

For example,the master-salve database config:

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:

Execute the target controller’s function by the router parse the url information.Is composed of four types of:

tradition router

pathinfo router

user-defined 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:

As above, we implemented a easy micro monolith structure.But how these module to communicate with each other? As follows:

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.

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:

Logics layer

A gateway example:

I built a gateway in the logics folder, structure as follows:

The gateway entrance class code as follows:

After the gateway be implemented, how to use this in the framework?I provide a user-defined’s class, we just register this in the UserDefinedCase class. for example:

Build Step

After build

After built success, there made dist folder and index.html in the public. This file will be ignore when this branch is not the release branch.

What’s the ORM(Object Relation Map)? In my opinion, ORM is a thought that build a relationship of object and the abstract things.The model is the database’s table and the model’s instance is a operation for the table.”Why do you do that, use the sql directly is not good?”, my answer:you can do what you like to do, everything is flexable, but it’s not be suggested from a perspective of a framework’s reusable, maintainable and extensible.

On the market for the implemention of the ORM, such as: Active Record in thinkphp and yii, Eloquent in laravel, then we call the ORM here is “ORM” simply. The “ORM” structure in the framework as follows:

DB example

Model example

What’s the service container?

Service container is difficultly understand, I think it just a third party class, which can inject the class and instance. we can get the instance in the container very simple.

The meaning of the service container?

According to the design patterns: we need make our code “highly cohesive, loosely coupled”. As the result of “highly cohesive” is “single principle”, As the result of “single principle” is the class rely on each other. General way that handle the dependency as follows:

The above code is no problem, but is not conform to the design pattern of “The least kown principle”, beacuse it has a direct dependence. We bring a third class in the framework, which can new a class or get a instance. So, the third party class is the service container, which like the role of ‘middleware’ in the architecture of the system.

After implements a service container, I put the Rquest, Config and other instances are injected into service in the singleton container, when we need to use can be obtained from the container, is very convenient.Use the following:

Inject the nosql’s single instance in service container when the framework loading, you can decide what nosql you need use whit the configuration. At present we support redis/memcahed/mongodb.

Some example:

This framework support swoole mode with the php extension swoole, just:

You can do some job in the jobs folder directly as follows:

Usually after we write an api, the api documentation is a problem, we use the Api Blueprint protocol to write the api document and mock. At the same time, we can request the api real-timely by used Swagger (unavailable).

I chose the Api Blueprint’s tool snowboard, detail as follows:

Api doc generate instruction

Api mock instruction

Based on the phpunit, I think write unit tests is a good habit.

How to make a test?

Write test file in the folder tests by referenced the file DemoTest.php, then run:

The assertion example:

cli script

Run the framework with cli mode, detail in the instruction.

build script

Build the application in the runtime/build folder, such as:

Command:

Run:

Web Server Mode:

Swoole Mode:

Get Help:

Use php cli OR php cli — help

How to Contribute?

After that, launch a PR as usual.

Add a comment

Related posts:

3 Quick Ways to Tell if Your Lead Magnet is a Dud

If you have a lead magnet on your website, you are already ahead of the competition. But is it performing how you expected? Are people signing up or ignoring it? There are a few ways to determine why…

A Million Little Things

I knew it was only a matter of time before you abandoned me.Before you got a hold of yourself and realized your worth. Realized you’re better than this. Better than me. I honestly thought I had…

Knowing what to outsource in your business

Running your own business can be tough. It can sometimes feel like there’s a never-ending task list and not enough hours in the day or week to finish it all! But you don’t have to do it all yourself…