Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to learn composer

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces how to learn composer, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

What if the system has different web applications but needs to share a lot of code?

When the system needs an extension, and this extension happens to be provided by someone on the Internet, how to use it.

How to upgrade, downgrade, and roll back PHP code

How to assign tasks and how to get multiple engineers to carry out development tasks together

I approached PHP in 2011, when V5.3.5 was released. At the language level, I don't think PHP has too obvious flaws. We have classes, SPL, anonymous functions, and etc on top of a rich library of web-oriented functions. These features (not "special" at all) are sufficient to support the coding requirements of a large project.

PHP5.3

However, when we are actually developing and really want to write code in PHP, we often encounter some crazy problems, which don't have much to do with PHP. But it's still a headache. When we want to write a website, we may need a CAPTCHA, but in most cases, I don't want to write a CAPTCHA myself. There are so many verification codes on the Internet that I naturally want to use them directly. But when I want to use it directly, what I have to do is:

Go to the search engine to search, and then see if each result has a suitable code, which can be used by me.

I found a class. At this time, I need to introduce this class into my project. Which directory should I put it in? What, autoload? Does it rely on any extensions? Will it need to be used on a higher version of PHP than I am now? These are all the problems I have to solve.

If I want to solve all the problems mentioned in 2, why don't I just write one?

Fidelyk it

Even with my own code, when I have multiple web applications (computer side, WAP side, api interface is normal), I certainly hope that they do not live in a project (directory), which will make it more difficult for me to view specified files, thus increasing my maintenance costs. But when I separate these web applications, there is so much general code (model, logic, auth.). What should I do with this code? I have modified a small logic of a web application, and I have to modify other apps. Either I can't remember, or even the smallest change will make people want to smash the computer, resign, and go out for fun.

Well, I'll split the code and use each other through autoload, so that more people can participate in the development, but the online situation is so complicated, in case something goes wrong with any piece of code, in case there is a special web application, the new code is not suitable for it. Maintenance is also a problem, to take over such a web application that depends on many other projects, it may be a lot of trouble to change the code slightly, because the autoload code is also very difficult for people to intuitively know which web application uses which code from other projects.

But in the face of PHP, I do not want to break the jar ah, after all, it is so convenient to write. I don't want to get out of the hole yet. But the above problems are not solved, anyway, I personally think that writing PHP is still a pretty crumbling thing. Let's take a look at how other languages solve this problem. JAVA's natural package mechanism allows it to use maven,node 's npm, and even Perl, which is older than PHP, has cpan. Shouldn't PHP have a package management mechanism?

Fortunately, these problems did not accompany my PHP for too long, because soon, PHP had Composer, and it was dawn.

Composer is a dependency management tool for PHP. It allows you to declare the code base on which the project depends, and it will install them for you in your project.

This is a brief introduction of Composer's Chinese official website.

I try to explain this sentence in terms of experience.

It allows you to declare the code base on which the project depends, that is, when you want to use what code you no longer need to copy, but to tell Composer through a declaration, just like going to a restaurant, you don't have to teach the chef how to do it, let alone do it yourself, and you don't have to eat your own plate, but tell the waiter what you want to eat, just tell it, of course, you can't tell him I have an upset stomach today. Make me something a little lighter and easier to digest. Anyway, I never order like this. I always have to tell them what you want to eat and the specific name of the dish. This is the difference from looking for code in a search engine before, you can't tell Composer by keyword, but tell it the name of the code base you want, WTF? Where do I know the name of the code, no one can know the name of other people's code, unless there is a place that contains all the code and provides a search function for us to find them and know their names.

That's what packagist.org does. We no longer have to look for luck in various search engines, where search keywords will not appear ads, Putian will not appear and JD will not appear.

It will install them for you in your project: after telling Composer, Composer will naturally serve us up, which is something that anyone can understand. We don't know which server the code we want is on, but Composer will help us download it locally. But here is another question: how to use it after downloading it? we know that if you want to use a file in PHP, you have to download include or require,Composer. How to eat this dish? do you need to prepare your own bowls and chopsticks? Fortunately, there is also a good thing, PHP-FIG, which does not produce code and does not provide solutions to any practical problems. The only thing he does is BB, so what does he BB? As I said above, because of the lack of some basic tools (such as Composer), it is difficult for PHP development to have some standards, such as coding specifications, such as directory structure, such as how to automatically load classes, such as how to type log, such as how to use caching, what will result? Different companies, different PHP programmers will begin to show their powers across the sea, of course, it doesn't matter for development in a short time, but in the long run, it will increase development costs and maintenance costs. when we change companies and take over a project, we have to understand the code from scratch, and even in a team, we will increase communication costs because there are no standards. So that's what PHP-FIG did: set standards. The standards he has set are:

Coding Specification (psr-1 psr-2)

Automatic loading Specification (psr-4)

Some common interfaces log (psr-3) cache (psr-6) http (psr-7)

These standards are described in detail on the official website. What we're talking about here is psr-4. I would like to elaborate a little bit here according to my own understanding and experience: the automatic loading of psr-4 is based on folders and namespaces, and we need to specify that a root directory corresponds to a root namespace. On this basis, we can find this PHP file under the root folder and load it by removing the namespace and class name outside the root namespace.

# root folder lib# root namespace model#file lib/A.phpnamespace model;class A {} # file lib/entity/B.phpnamespace mode\ entity;class B {} # file demo.php$a = new\ model\ A (); $b = new\ model\ entity\ B ()

Composer implements the ability to generate automatically loaded classes based on specified standards (such as psr-4) and mapping relationships (such as lib- > model in the code). In fact, Composer provides these standards:

The way files specifies the path of PHP files, which loads these files every time it is requested, and is suitable for PHP files of some general functions

Classmap is smarter than files and can specify a folder or a file to load automatically. the disadvantage is that even if a folder is specified, a file added to this folder requires Composer to regenerate an autoload file, which is suitable for classes or class libraries that cannot use psr-4, such as client with a third-party interface. This client may have existed before the emergence of psr-4 rules. So we still want to use Composer for management, so we can use this way.

The predecessor of psr-0 psr-4, he used to be out of date, just forget I said it.

Psr-4 as I described above, adding one or more files in this way does not require the autoload file to be regenerated, because it is loaded according to the mapping between the namespace and the folder.

So what are the benefits of implementing this with Composer?

We don't need to write any autoload files ourselves, and the standard is easy to understand and accept, and the cost of maintaining and learning code is reduced.

As long as the third-party library we need is also handled by Composer, we only need the require package, then the code that loads this third-party library will also be handled by Composer, and we have a super autoload file.

So, all we have to do is learn to deal with Composer and start to enjoy the code of developers around the world.

As described above, Composer is like a robot cat. It gives you whatever you want, so the interaction is similar to the SQL statement, telling it what you want and then it gives you the results. So what we need to do is to describe the requirements, that is, to be a product manager.

{"name": "fmw/test", "description": "fmw test", "authors": [{"name": "zzc", "email": "2272713550@qq.com"}], "repositories": [{"type": "composer" "url": "http://package.fmw.com"}]," version ":" 1.0.106 "," require ": {" fmw/other-layer ":" 1.* "," fmw/common ":" 1.* "}," require-dev ": {" php-console/php-console ":" ^ 3.1 " "phpdocumentor/phpdocumentor": "2.*"}, "autoload": {"psr-4": {"model\": "src/"}

The above code is a composer configuration file that I have used, and you can see that this is a standard json. Let's take a look at each key of this json:

Name and description are the names you give to this php project. When this project is just a web project, these two are not really very important, but this project is actually a released code base, which is critical. Name needs to be unique, and description needs a sentence to describe the role of this package.

Authors is equivalent to a declaration of sovereignty, and there can be multiple

Repositories is equivalent to the warehouse where the code base you need to download is located. By default, there will be a global warehouse. I won't talk about it here. One of the URLs above is described. Add one here because if you have a private warehouse (some code is not suitable for public storage), you can declare it here.

Version is the version number, this is a cross-era function, with this, PHP programmers can also brush the version number ah!

Require is the above described a lot of functions, to solve the pain points I said, through the "name": "version" declaration, there can be multiple, require later use the composer install command composer will download the code and automatically load

Require-dev is used in the same way, but with different functions. It is used to declare packages that are only used at development time, such as tests, documentation, and so on.

If there is an introduction on autoload, there will be no nonsense.

After the above work is done, execute composer install. We can see that a vendor folder has been generated under the folder at the same level as composer.json. If we create a new php file and import it into the autoload.php file under vendor, we can use the package and the php file of autoload declared by ourselves.

# index.php

Include'. / vendor/autoload.php'

At this point, even if we can use composer, we don't know how to use composer, but there are still some issues we'd like to discuss.

For example, some code is not suitable to be placed in an open warehouse, but we still want to use it in the form of packages. After all, it is easy to divide the work within a company, and it is convenient for each PHP programmer to maintain several packages, so it is very important to establish an internal code repository. At this point, satis, an official tool provided by Composer, can come into play.

Simple static Composer repository generator

This is its introduction, a simple Composer repository generator. The steps to use it are as follows:

Execute php composer.phar create-project composer/satis-stability=dev-keep-vcs in the appropriate directory (provided you have followed Composer)

Create a new satis.json instance as follows

{"name": "My Repository", "homepage": "http://packages.dev.com"," repositories ": [{" type ":" vcs "," url ":" http://git.dev.com/maxincai/package1.git"}, {"type": "vcs", "url": "http://git.dev.com/maxincai/package1.git"},] "require": {"maxincai/package1": "*", "maxincai/package2": "*",}

Execute php bin/satis build satis.json public/ (public is the directory where all packages are stored)

It would be nice to publish the public directory as a web service

When using it, you only need to add one more item to repositories (as I did in composer.json above), and then introduce the package.

Thank you for reading this article carefully. I hope the article "how to learn composer" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report