In terms of PHP package management, the development of PHP is very slow. As a result, few programmers are found to use tools such as PEAR. On the contrary, most developers choose their preferred frameworks to Process Code, such as DB interaction, ORIMS, Oauth, and AmazonS3 integration. The disadvantage is that when converting the framework (or you do not need to return the framework at all)
In terms of PHP package management, the development of PHP is very slow. As a result, few programmers are found to use tools such as PEAR. On the contrary, most developers choose their preferred frameworks to Process Code, such as DB interaction, ORIMS, Oauth, and Amazon S3 integration. The disadvantage is that when converting the framework (or you do not need to return the framework at all)
In terms of PHP package management, the development of PHP is very slow. As a result, few programmers are found to use tools such as PEAR. On the contrary, most developers choose their preferred frameworks to Process Code, such as DB interaction, ORIM's, Oauth, and Amazon S3 integration.
The disadvantage is that when you change the framework (or do not need to return to the framework at all), it feels like a nightmare. because it involves using new tools, you must learn everything in it, this is not simple. OK,ComposerTo help you solve these problems.
Introduction
Composer solves the problem by positioning itself as "sticking together all projects. This means that the package can be written, developed, and shared in a certain format, and other developers can easily insert it into the application.
This article will explain how to install and use the Composer package. At the end of the article, you can insert the code block into any framework for experience. You can use CodeIgniter, FuelPHP, Laravel, Symfony2, Lithium, Yii, Zend, and so on.
Install
Composer contains two major logical parts: one is used to store packages, and the other is a command line application to help you discover, download, update, and share code.
- $ cd/path/to/my/project
- $ curl -s http://getcomposer.org/installer| php
In the project list, a composer. phar file contains all the logical code line tools. You can run the following code to determine whether the installation is successful.
- $ php composer.phar
After this command is executed, all available commands are displayed.
I personally suggest you use this command:
- $ sudo mv composer.phar /usr/bin/composer
Move this file to the bin directory, which allows you to simplify the command.
- $ composer about
If you are running on Windows, you can download this file and install it through the PHP parser, wherever you are.
Parse the composer. json File
If you are a Ruby programmer, you will think this file is very similar to the Gemfile file, or if you are a Node programmer, you will think it is very similar to the package. json file. Similarly, Composer uses the composer. json file to specify settings and encapsulation based on your application requirements.
In most basic forms, the composer file looks like this:
- {
- "require": {
- "kriswallsmith/assetic": "*"
- }
- }
A "assetic" package is required to create and specify any version through "kriswallsmith. You can also specify a special version. You can use the following command instead:
- "kriswallsmith/assetic": "1.0.3"
You can even use this method:
- "kriswallsmith/assetic": "1.0.*"
This has some minor changes, but it will not be upgraded to 1.1.0. Programmers need to pay attention to the slight changes on the interface.
Installation requirements
Now, one or more packages under your composer. json file can be run at this time:
- $ php composer.phar install
Or, if you have listened to my suggestions, you only need to run the following command on a Unix machine:
- $ composer install
You will find that the file is being downloaded and placed in the vendors folder under the application root directory. The following configuration can also be used for this logic:
- {
- "require": {
- "kriswallsmith/assetic": "1.0.*"
- },
- "config" : {
- "vendor-dir" : "packages"
- }
- }
Automatic Loading
Automatically loaded in PHP is a bit messy. as developers, they have their own operations. For example, the Smarty package uses its own automatic load. Some developers put multiple classes in one file, or the file name is in lower case, which is too casual!
The official PHP community has created PSR-0 standards to regulate these casual practices. By default, Composer supports this standard. Composer built-in PSR-0 automatic loading mechanism, add the following line of code in the project:
- include_once './vendor/autoload.php';
Obviously, if the directory of the autoload. php file changes, you also need to make corresponding changes in the Code.
Below, you can use the following code in the application:
-
- use Assetic\Asset\AssetCollection;
- use Assetic\Asset\FileAsset;
- use Assetic\Asset\GlobAsset;
- $js = new AssetCollection(array(
- new GlobAsset('/path/to/js/*'),
- new FileAsset('/path/to/another.js'),
- ));
-
- // the code is merged when the asset is dumped
- echo $js->dump();
This is an example of using Assetic. Of course, there are also a lot of namespace code here, but this is done to avoid conflicts between packages.
The essence of naming convention for PSR-0 is:
- \ \( \)*
The following example shows the Buzz HTTP package:
- $browser = new Buzz\Browser;
- $response = $browser->get('http://www.google.com');
- echo $browser->getLastRequest()."\n";
- echo $response;
It looks like a beautifying file_get_contents (), but it processes all types of intelligent logic and processes HTTP Response/Request in the background. You will also find that the namespace syntax is not that strong.
Real world
Currently, most PHP storage relies on the main code library. If you use the Facebook SDK, for example, you just push the version from the GitHub or zip file to your code by copying and pasting it, and then put it in your version control system, will change.
The version and your code are only put in the static file. In a sense, you may forget to upgrade it if you are concerned that Facebook has released a new version. The latest file is displayed on the top.
With Composer, you do not need to pay attention to version changes. You only need to run the updates, and all the updates to be updated will be automatically updated. But why is there a lot of code in your repository? Don't you need them there?
The simplest way is to add vendpoints to your "Ignore" list (for example, gitignore) and let your code go completely away. When deploying code, you only need to run composer install or composer update.
If you want to use it more skillfully, You can manually run the entire process. If you have cloud hosting, you can set hooks to run once the code is released.
Summary
In the future, you will see more Composer and various frameworks have begun to provide multi-level integration. FuelPHP will build a Composer package. CodeIgniter provides automatic loading and is widely used in Symfony2.
Using Composer to add related packages to your project is a good way, you do not need to install the PECLI extension or copy and paste a series of files. That method is outdated and a waste of time. (Compilation: Zhang hongyue)
Market@csdn.net.