This topic was previously done in the micro blog company internal technology sharing, here to share with you.

Everyone is familiar with the introduction of open source projects hosted on GitHub or other platforms, but what about the modularization of internal projects? This may be a headache for many friends.

Let’s talk about the evolution process of PHP modular development. Before GitHub, the main way for us to get and share code is blog, CSDN or blog park in China, there are many, many others, we all copy from the article to their own projects. It seems pretty primitive now, but there weren’t many alternatives back then. The resulting phenomenon is that a piece of code appears in N projects, probably the most seen are the few lines that obtain the client IP, more than tens of thousands of times on the Internet. It’s still in many projects:

Does that sound familiar?

This way of introducing code has a number of drawbacks: it’s not secure, for example, because many people just copy and paste and use it without taking the time to verify that it’s actually secure. The other problem is that it is not synchronized. You can copy it from someone else today and use it. Later the author finds a bug and fixes it and updates the article without informing you, and you cannot remember where the code came from to check for updates.

How did we introduce the code before Composer? In addition to the copy and paste mentioned above, there is PEAR in PHP, but I haven’t used it since I used it twice, a feeling I can’t tell.

If you don’t believe it, you can look for some old projects. In the projects before Composer, you will find a lot of duplicate code, various organizational formats and various standard writing methods. This is one of the reasons why Composer was born.

Composer brings many benefits to us:

  • Modularization reduces code reuse costs
  • Unified organization of third-party code
  • A more scientific version has been updated

These three are more important features, based on GitHub shared code to solve the traditional way to introduce a variety of problems.

Let’s start with a little bit of Composer basics.

The implementation structure of Composer is relatively simple. http://Packagist.org is the official source of Composer, and its data is based on GitHub and other code hosting platforms. You can use the local command line tool of Composer. Install and update dependencies for data information based on http://Packagist.org. Installing Composer locally is very simple and can be done in the following ways:

Note that the composer installation directory must be within the environment variable $PATH to use the composer command globally.

Let’s talk about how to create a Composer package.

The steps are simple: create a directory, and then use the command ‘Composer init’ within the directory to initialize the package as prompted.

Then complete your code and configure your import method in the composer. Json file. Then how do we test the code we’ve already written?

We need to create a test project anywhere else (not in the package directory we just created). For example, here we create a directory called ‘my-package-test’ and initialize the project in composer init. The package is not released to packagist yet, so we cannot install composer require directly. We need to tell composer where to load our package information:

$ composer config repositories.foo path /Users/overtrue/www/foo/bar
Copy the code

With this command, we add a project source in the Composer. Json block.

Then we add package dependencies:

$ composer require foo/bar:dev-master -vvv
Copy the code

This completes the package installation, and you’ll notice that it just creates a soft link to the package directory, so you can modify the code directly under Vendor /foo/bar while testing, which speeds up your development.

For more details, see the composer. Json file:

The most important parts to care about are the top three parts: package name, dependencies, and autoloading.

Just now we mentioned the package installation, there are two main ways to install the dependency package:

Manual writing is not recommended and is prone to errors such as extra commas, but you can verify this by using the following command after writing:

$ composer validate
Copy the code

Updating dependencies is very simple:

Although we covered semantic versions in the last article, here we go again:


When we rely on a package, many students have been in a state of confusion about the version of the dependency, so after reading the next page, you suddenly understand, the first two symbols: “~” and “^”

The range of version numbers is then written in various ways:

There are also flags that contain stability:

The most important thing that many students don’t know about production environment is version locking. Many people are debating whether to upload composer. Lock to the code base. I can give you a very simple way to judge:

If your code is a project, upload it. If it’s a toolkit for everyone, don’t.

When composer install is executed in a directory where composer. Lock already exists, package dependencies are not analyzed. It is simply downloaded directly from the address described in Composer. A new version of composer. Lock will be installed as long as composer. Lock has not been changed. Composer update will update composer. Lock, so don’t mess with composer update.

How to release the package once it is developed? The open source approach works like this:

Please consider the last sentence for your discretion.

Another distribution method is closed source, which is a package used in-house and uploaded to GitLab or other private code hosting platform. There are two ways to do this:

  1. The easiest way to do this is to add repositories in Composer. Json to specify the repository location directly with VCS

The downside of this is that when you have a lot of packages, you have to add them all in composer. Json.

2. Install Packagist’s own similar service. Packagist officially offers two versions: Toran, for a fee. Satis is an open source solution, but it is more manual.

Private packages have a point to note: authorization, private packages are certainly required to access the authorization, here because the scheme is not very general, we according to their own scenarios to solve the good.

In addition, there are some pain points that do not know when will be resolved:



Fortunately, Laravel China has provided me with the most stable and useful source of Chinese mirroring/Packagist full mirroring in China.

To sum up:

Wechat is not suitable for talking about code details, I prefer to provide ideas.

In modern PHP development, Composer has become indispensable. It really speeds up development and saves development costs. If you are still struggling to use Composer, you should think again.

The title of this paper is modular development, the main content of the package creation and testing, as well as public package and private package release scheme. It’s not going to help you figure out how to break things down, it’s going to have to be based on your long experience, but there are a few things you can share:

  1. Do not over design, many students who think they are very NB I do not use what I have learned is not happy, up on the database sub-table, UUID master key and so on, the project has been operating for several years a table has not reached 1 million records, is also strong enough.
  2. Don’t early design, real NB, the evolvement of the architecture is the preliminary design does not come out, of course not to say that without the need for design, appropriate according to the actual situation is good, don’t take a “must”, “million” and “billions” in these units, may fail to your project that you haven’t been to any one order of magnitude. As the project improves.
  3. Focus on cost first, many students think it is performance, No! The real cost of a technical team is manpower, so development efficiency is a priority. Last time when my article was posted on Zhihu, someone forced me to interpret my “don’t pay attention to performance first” into “performance is not important”, which is also strong enough. Chinese may have something to do with animal husbandry. Mathematics normal a bit of person can calculate, a server how much money? How much is a tech employee? You pay $200,000 for a server for permanent assets, $200,000 for an employee? Six months salary? A year’s salary?

That’s all for today. Last time, a friend posted an article to today’s headlines without my authorization. If you see it again today, please don’t do such a childish thing. Could you at least sign your name…

Please forward to the circle of friends, help me open the comment function as soon as possible, love you!

Please note: Pretend I can write code