once

We all have periods of confusion, but here’s how I’ve changed the way I’ve organized my code since I started PHP development.

  • Early stage: All the code in one controller
    • It used to be a simple understanding of MVC
  • Mid-stage: Business code abstracts part of the model layer model
    • Starting to feel like the Model layer should do something
  • Later stage: business code controller, the model layer only writes the CURD method of DB
    • Complex business code makes controllers too large and difficult to maintain

The present

Based on previous coding experience, I have made the following changes in my current coding mode:

More rational layering

  • Controller Exposed Resources
  • Business code to the logical layer
  • The model layer only writes db curd methods

Degree of control, everything is flexible, is not always so, think enough simple logic or “business code controller, model layer only write DB curD method”.

Dependency injection, lazy loading, pre-middleware

  • Dependency injection containers are important, coupled with lazy loading to reduce code, decouple dependencies, and improve performance
    • For containers you can refer to my previous simple implementation easy-php.tigerb.cn/#/?id=servi…
  • The front-facing middleware injects our dependent classes
    • The simple front-facing middleware I implement, the example mounts a gateway to the controller before easy-php.tigerb.cn/#/?id=mvc-t…

Componentized code

First of all, we need to use Composer to split and componentize our code. Simple Composer require in business can reuse our business code. However, there is a problem if you use packagist.org, you need to open source the code. The non-business tool classes are ok, and open source is a good thing, but the business sensitive components are problematic, so we need to build a private packagist, private packagist example: Packagist.tigerb. cn/. For details, see the end of this article.

conclusion

I am currently working on a project where I will organize my PHP code in the following ways:

  • Composer is componentized code that relies on open source or proprietary packagist
  • The pre-loaded middleware relies on components injected with Composer require in a lazy manner
  • Controller Exposed Resources
    • Elegant parameter verification tool class
    • Using a try… catch… finally…
    • The response should be as restful as possible, such as error code:400,404,500…
  • Business code to logical layer (not necessarily)
    • Complex business code is modeled before it is written. Modeling organizes our code and applies some design patterns. For modeling, refer to the Observer decorator pattern summarized in my PHPer monthly work article
  • The model layer only writes db curd methods
    • Unshakable principles

conclusion

There are many deficiencies, here is just to share their own ideas, good we can learn from, bad hope we point out more.

THX~

The appendix

Satis builds a proprietary packagist process by following the following steps:

step 1:

composer create-project composer/satis:dev-master –keep-vcs && cd satis

step 2:

touch satis.json

The content of the satis.json file is as follows

{
  "name": "packaglist-tigerb"."homepage": "http://packaglist.tigerb.cn"."repositories": [{"type": "vcs"."url": "http://github.com/tigerb/easy-mipush" },
    { "type": "vcs"."url": "http://github.com/easy-framework/easy-router"}]."require-all": true
}
Copy the code

step 3:

PHP bin/satis build. /satis.json < your web servser project directory, such as my path/MNT/WWW >

step 4:

To configure the nginx server configuration, restart nginx

How to use it?

Json, and add a new repository address. Examples are as follows:

{
    "name": "tigerb/test"."authors": [{"name": "TIGERB"."email": "[email protected]"}]."repositories":[
        {"type":"composer"."url": "http://packagist.tigerb.cn"}]."config": {
        "secure-http": false
    },
    "require": {
        "tigerb/easy-mipush": "^ 0.1.0 from"}}Copy the code